Jump to:

1971 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » I want to set up custom validation for my form page

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

Page: 1
Go to End
Author Topic: 376 Views
  • leafchild
    Avatar
    Community Member
    41 Posts

    I want to set up custom validation for my form page Link to this post

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: I want to set up custom validation for my form page Link to this post

    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;
       }
    }

    376 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.