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

I want to set up custom validation for my form page


Go to End


2 Posts   1430 Views

Avatar
leafchild

Community Member, 41 Posts

18 November 2011 at 1:41pm

I have my form code like this below

 function __construct($controller, $name) {
      $fields = new FieldSet(
         new TextField('Name', 'name', 'first','20'),
         new EmailField('Email', 'Email address', '', '100'),
         new TextField('Phone', 'Phone number','','20'),
      );

    $actions = new FieldSet(new FormAction("login", "Log in"));
	  
      $validator = new RequiredFields('Name', 'Email');

      parent::__construct($controller, $name, $fields, $actions, $validator);
   }

I want to add more vilification for this form.
I want to add phone# vilification.
How and where I can add vilification code to do that and show an error message to form(view file) if there is error from phone number?

thanks

Avatar
swaiba

Forum Moderator, 1899 Posts

19 November 2011 at 12:14am

you can create and assign your own "extended" validator. This is a simple example...

class MyValidator extends RequiredFields {
	function php($data) {
		$bRet = parent::php($data);

		if (empty($data['MyField'])) {
			$this->validationError('MyField','MyField is required',"required");
			$bRet = false;
		}

		return $bRet;
	}
}