Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

Validate Confirm Email in custom form


Reply


2 Posts   965 Views

Avatar
Sindhura

Community Member, 2 Posts

14 February 2013 at 10:32pm

Hi,

How do you validate a confirm email field in a custom email form? Pasted below is the code that I am using.

function ContactForm() {
// Create fields

$fields = new FieldSet(
new TextField('Name', 'Name*'),
         new NumericField('Number', 'Number in Party*'),      
         new DropdownField('Time', 'Preferred Time', array(
            '12:30pm' => '12:30pm',
'1:00pm' => '1:00pm',
'1:30pm' => '1:30pm'
       )),
       new TextField('Telephone', 'Contact number*'),
         new EmailField('Email', 'Email address*'),
         new EmailField('CEmail', 'Confirm email address*'),
);

// Create action
$actions = new FieldSet(
new FormAction('SendContactForm', 'Send')
);

// Create Validators
$validator = new RequiredFields('Name', ,'Telephone', 'Email');

return new Form($this, 'ContactForm', $fields, $actions, $validator);
}

Avatar
Devlin

Community Member, 217 Posts

15 February 2013 at 2:40am

@see http://doc.silverstripe.org/framework/en/topics/form-validation

Either you check both emails in the SendContactForm method and redirect back on error. Or you subclass RequiredFields.

My favorite is subclassing, because the action method should not be called unless the form is properly validated and subclassing works for ModelAdmin too.