Jump to:

1976 Posts in 1306 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » How to trigger a php function for custom server side form validation?

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

Page: 1
Go to End
Author Topic: 1545 Views
  • Tama
    Avatar
    Community Member
    119 Posts

    How to trigger a php function for custom server side form validation? Link to this post

    Howdy

    I've been searching around the forums and documentation for a while but am still a bit lost about this. I've got a form which I want to validate server-side by using my own php function.

    There's the "php" method in the RequiredFields class: http://api.silverstripe.org/2.4/forms/validators/RequiredFields.html - but I can't find any example of how this would work in the wild.

    On the most basic level I'm looking for an example of how to get something like the code to work. This is where a value from a TextField is sent to a function which then returns a message and either does or doesn't validate the field.

    $fields = new FieldSet(
    new TextField('Number', 'Enter a Number', '' )
    );

    function ValidateNumber($Number){
    if($Number == 10){
    $Message = "You have entered the right number";
    $Validate = true;
    } else {
    $Message = "You have entered the wrong number, try again";
    $Validate = false;
    }
    }

    Can someone please show me an example of how this would work inside the sapphire framework?

    Cheers
    Tama

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: How to trigger a php function for custom server side form validation? Link to this post

    Does this help? (from one of my forms with validation)...

    class MyFormPage_Controller extends Page_Controller
    {

       function MyForm()
       {
          $fields= new FieldSet(
             new TextField('MyTextField')
          );

          $actions = new FieldSet(
             new FormAction('ProcessFrom', 'Join')
          );

          $validator = new RequiredFields(
             'MyTextField'
          );

          $form = new Form($this, 'MyForm', $fields, $actions, $validator);
          
          if(Session::get("MyFormData"))
          {
             $previous_value = Session::get("MyFormData");
             Session::clear("MyFormData");
             $form->loadDataFrom($previous_value);
          }

          return $form;
       }
       

       function ProcessFrom($data,$form)
       {
          $bValid = true;
          
          Session::set("MyFormData", $data);

          if($data['MyTextField'] != 'somevalue')
          {
             $form->addErrorMessage('MyTextField','MyTextField is not "somevalue"','required');
             $bValid = false;

          }

          if (!$bValid)
          {
             //failed validation
             return Director::redirectBack();
          }

          //passed validation
          
          //do stuff with form data
          
          return Director::redirect(Director::baseURL(). $this->URLSegment.'?posted=1');
       }
    }

    1545 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.