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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Validate Confirm Email in custom form


Go to End


2 Posts   2616 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, 344 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.