Jump to:

2001 Posts in 1422 Topics by 615 members

Form Questions

SilverStripe Forums » Form Questions » Validating form

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

Page: 1
Go to End
Author Topic: 1580 Views
  • anujkryadav
    Avatar
    Community Member
    30 Posts

    Validating form Link to this post

    I have created a form using the below code which is working fine.
    I need to validate the fields.Please help me to validate the form

    RegistrationForm.php

    <?php
    class RegistrationForm extends Page {
    }

    class RegistrationForm_Controller extends Page_Controller {

    // Make sure you set this to the right group.
    // See http://doc.silverstripe.com/doku.php?do=show&id=recipes%3Aforms
    private $defaultGroupID = 2;

    /**
    * This function lets you put a form on your page, using $Form.
    */
    function Form() {

    return new Form($this, "Form", new FieldSet(

    // List your fields here
    new EmailField("Email", "Email address"),
    new PasswordField("Password", "Create a Password"),
    new TextField("FirstName", "First name"),
    new TextField("LastName", "Last name"),
    new TextField("Phone", "Phone"),
    new TextField("Company", "Company"),
    new TextField("Address1", "Address 1"),
    new TextField("Address2", "Address 2"),
    new TextField("City", "City"),
    new TextField("State", "State"),
    new TextField("Zip", "Zip"),
    new TextField("Country", "Country"),
    new ReadonlyField("IPAddress","IPAddress",$_SERVER['REMOTE_ADDR'])

    ), new FieldSet(

    // List the action buttons here
    new FormAction("SignupAction", "Submit")

    ), new RequiredFields(

    // List the required fields here: "Email", "FirstName"
    "Email", "Password", "FirstName", "LastName", "Phone", "Company", "Address1", "City", "State", "Zip", "Country"

    ));
    $Form->setTemplate('RegistrationForm');
    }

    /**
    * This function is called when the user submits the form.
    */
    function SignupAction($data, $form) {

    // Create a new Member object and load the form data into it
    $member = new Member();
    $form->saveInto($member);

    // Write it to the database. This needs to happen before we add it to a group
    $member->write();

    // Add the member to group. (Check if it exists first)
    if($group = DataObject::get_one('Group', "ID = 2")) {

    $member->Groups()->add($group);
    // Redirect to a page thanking people for registering

           // COMMENTING BY ANUJ
           //Set data
    /* $From = $data['Email'];
    $To = "email address of who gets the email, 2nd email addres";
    $Subject = "Registration Request";
    $email = new Email($From, $To, $Subject);
    //set template
    $email->setTemplate('RegistrationFormEmail');
    //populate template
    $email->populateTemplate($data);
    //send mail
    $email->send(); */
             
             // COMMENTING ENDS HERE BY ANUJ

    //$e = new Email();
    //$e->setTemplate("RegistrationForm");
    //$e->To = "terry@thestoreinsider.com";
    //$e->Subject = "Registration Request";
    // $e->Body = populateTemplate($data);

    //$e->Body = $member;
    //$e->send();
    Director::redirect('thanks-for-registering/');

    }else{

    // Redirect to a failure page
    Director::redirect('registration-failed/');

    }

    }
    }

    ?>

    Please help me in validating the form fields.
    Note.my form is working fine and its working the way i want.it is entering user under a particular group

  • anujkryadav
    Avatar
    Community Member
    30 Posts

    Re: Validating form Link to this post

    I am giving the code of php page again

    <?php
    class RegistrationForm extends Page {
    }

    class RegistrationForm_Controller extends Page_Controller {

    // Make sure you set this to the right group.
    // See http://doc.silverstripe.com/doku.php?do=show&id=recipes%3Aforms
    private $defaultGroupID = 2;

    /**
    * This function lets you put a form on your page, using $Form.
    */
    function Form() {

    return new Form($this, "Form", new FieldSet(

    // List your fields here
    new EmailField("Email", "Email address"),
    new PasswordField("Password", "Create a Password"),
    new TextField("FirstName", "First name"),
    new TextField("LastName", "Last name"),
    new TextField("Phone", "Phone"),
    new TextField("Company", "Company"),
    new TextField("Address1", "Address 1"),
    new TextField("Address2", "Address 2"),
    new TextField("City", "City"),
    new TextField("State", "State"),
    new TextField("Zip", "Zip"),
    new TextField("Country", "Country"),
    new ReadonlyField("IPAddress","IPAddress",$_SERVER['REMOTE_ADDR'])

    ), new FieldSet(

    // List the action buttons here
    new FormAction("SignupAction", "Submit")

    ), new RequiredFields(

    // List the required fields here: "Email", "FirstName"
    "Email", "Password", "FirstName", "LastName", "Phone", "Company", "Address1", "City", "State", "Zip", "Country"

    ));
    $Form->setTemplate('RegistrationForm');
    }

    /**
    * This function is called when the user submits the form.
    */
    function SignupAction($data, $form) {

    // Create a new Member object and load the form data into it
    $member = new Member();
    $form->saveInto($member);

    // Write it to the database. This needs to happen before we add it to a group
    $member->write();

    // Add the member to group. (Check if it exists first)
    if($group = DataObject::get_one('Group', "ID = 2")) {

    $member->Groups()->add($group);
    // Redirect to a page thanking people for registering

          
    Director::redirect('thanks-for-registering/');

    }else{

    // Redirect to a failure page
    Director::redirect('registration-failed/');

    }

    }
    }

    ?>

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Validating form Link to this post

    RequiredFields needs to be passed an array to iterate over, not just strings. Change your RequiredFields code to

    new RequiredFields(arrray("Email", "Password", "FirstName", "LastName", "Phone", "Company", "Address1", "City", "State", "Zip", "Country"))

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