I have been struggling to understand how the spamprotection can be added to my forms.
This is not for protection on page-comments etc, but on a simple "Contact Us" form that I have added.
I cannot figure out from the documentation how to get either Recaptcha or Mollum to work.
Judging by the number of page hits I'm seeing in that part of the forums, I guess I'm not the only one puzzled.
The steps below basically build on the format indicated by the tutorials.
So I created a "ContactusPage.ss" file in themes/mytheme/templates/Layout which calls a ContactusForm function
...
<div id="Content" class="typography">
<% include Breadcrumbs %>
$Content
<div id="Contact">
$ContactusForm
</div>
</div>
...
I then created this function in mysite/code as "ContactusPage.php"
<?php
class ContactusPage extends Page {
static $db = array(
);
static $has_one = array(
);
}
class ContactusPage_Controller extends Page_Controller {
function ContactusForm() {
// Create fields
$fields = new FieldSet(
new TextField('Name','My name is ...'),
new EmailField('Email','My email address is ...'),
new TextField('Subject','Message Subject:'),
new TextareaField('MessageText', 'My message:', '5', '35')
);
// Create actions
$actions = new FieldSet(
new FormAction('doContactusMail', 'Send my message')
);
$requiredFields = new RequiredFields(
"Email", "Name", "MessageText"
);
//$protector = SpamProtectorManager::update_form($ContactusForm, 'Message');
//if($protector) $protector->setFieldMapping('Name', 'Email');
return new Form($this, 'ContactusForm', $fields, $actions,$requiredFields);
}
function doContactusMail($data, $form) {
$email = new Email();
$email->from = 'contactusform@localhost';
$email->to = 'webbuilder@localhost';
$email->subject = 'Web Contact: '.$data['Subject'];
$email->body = 'My name is '.$data['Name'].', ('.$data['Email'].') and my message is '.$data['MessageText'];
$email->sendPlain();
Director::redirectBack('sent/');
}
}
?>
All that works nicely, but I cannot add a captcha to it!
I have tried both Recaptcha & Mollom in what follows
Steps:
Added module spamprotection module (v0.2) to main directory
added Recaptcha FormField Module (v0.2) to main directory
added mollum (v0.2) to the main directory
(Also added userforms to main directory as a precaution)
Following the advise at: http://doc.silverstripe.com/doku.php?id=modules:recaptcha
I edited mysite/_config.php to add:
SpamProtectorManager::set_spam_protector("RecaptchaProtector");
// get a key at http://recaptcha.net/api/getkey
RecaptchaField::$public_api_key = '<mypublic_key>';
RecaptchaField::$private_api_key = '<myprivate_key>';
// field creation
$recaptchaField = new RecaptchaField('MyCaptcha');
but that throws errors
"Fatal error: Class 'RecaptchaField' not found in /var/www/Silverstripe/mysite/_config.php on line 26"
So this is obviously not right and various hacks failed, so I abandoned Recaptcha and tried the alternative ... Mollom
in mysite/_config.php I added:
Mollom::setPublicKey('<mypublic_key>');
Mollom::setPrivateKey('<myprivate_key>');
SpamProtectorManager::set_spam_protector('MollomSpamProtector');
(no errors ... so far so good)
The documentation
http://doc.silverstripe.com/doku.php?id=modules:spamprotection
suggests adding a protector ... but is vague as to where
"the following code needs appear after the form creation."
So inserting the following into ContactusPage.php was a failure ...
$protector = SpamProtectorManager::update_form($ContactusForm, 'Message');
if($protector) $protector->setFieldMapping('Name', 'Email');
Error:
[Notice] Undefined variable: ContactusForm
So now my lack of OOP-nous has caught me ...
Is there a smart cookie out there who is able to see how I can get some captchas going?
I would prefer Recatcha to mollom, but at the moment I have ... nothing!