Not used forms much up until now. I've read a fair amount on these forums, but I'm still a bit confused as to how I can add my own validation for a custom field? What I'm trying to do is implement a very simply spam protector that I often use in html forums. It's not very secure admittedly, but it suffices for very low volume sites and looks less bulky than full-on captcha. Basically it outputs random string that user re-enters to validate. Here's what I have so far:
function contactUsForm() {
$ranstr = chr(mt_rand(65,90)) . chr(mt_rand(65,90)) . chr(mt_rand(65,90)) . chr(mt_rand(65,90)) . chr(mt_rand(65,90)) . chr(mt_rand(65,90));
// Create fields
$fields = new FieldSet(
new TextField('Name'),
new TextField('Email'),
new TextField('Phone'),
new TextareaField('Message'),
new TextField('SpamProtect', 'Enter first 3 letters of the following: ' . chunk_split($ranstr,1,' ') )
);
// Create actions
$actions = new FieldSet(
new FormAction('doContactUs', 'Send')
);
//valididate fields
$validator = new RequiredFields('Name', 'Email', 'Message', 'SpamProtect');
return new Form($this, 'contactUsForm', $fields, $actions, $validator);
}
Now adding my custom field to the validator above requires a value to be entered but of course does not do any checking. So I'm presuming I need to code a custom validator rule just for this custom field, but I'm slightly confused as to where I do this?
Any help appreciated!