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 /

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

I want to set up custom validation for my form page


Go to End
Reply


2 Posts   597 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, 1826 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;
	}
}