Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » going back to form with populated fields

Our old forums are still available as a read-only archive.

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

Page: 1
Go to End
Author Topic: 2084 Views
  • g
    Avatar
    Community Member
    22 Posts

    going back to form with populated fields Link to this post

    Working on a registration system where user must create an account. All custom code, no cms for this component. I have a form that user fills out. When they submit, I check to see if the username exists. If not i insert into table. If it does exist I need to go back to the form with the fields populated with the values they entered. How do I go back to form with populated fields?

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: going back to form with populated fields Link to this post

    The forum signup saves the valid data to a session - Session::set("FormInfo.Form_RegistrationForm.data", $data); before it takes the user back. Then you just need to edit your form so before you return it you check if that cookie exists and if it does load the data from it

    ..
    $form = new Form.....
    $data = Session::get("FormInfo.Form_RegistrationForm.data");
    if(is_array($data)) {
       $form->loadDataFrom($data);
    }
    return $form;
    ..

  • g
    Avatar
    Community Member
    22 Posts

    Re: going back to form with populated fields Link to this post

    Thanks for responding.

    I had a little trouble getting it to work as you described. Your response did set me in the right direction however I feel my solution can be improved. I've downloaded the forum code and will check it out. I ended up setting session variables for each of the form fields plus one more session value to indicate failed submission , then using Director::redirectBack(); to take me back to the form. I also modified the original form method to populate the form fields with values from the session variables if there was an indicated error. It works and that's the important thing.

    class myForm_Controller extends Page_Controller {

       function Form() {
       
          
          if(Session::get("RegistrationError"))
          {
             Session::set('RegistrationError',false);
             return new Form($this, "Form", new FieldSet(
    // List the your fields here
    new TextField("FirstName", "First name",Session::get("FirstName")),
    new TextField("LastName", "Last Name",Session::get("LastName")),
        new TextField("Address1", "Address 1",Session::get("Address1")),
        new TextField("Address2", "Address 2",Session::get("Address2")),
        new TextField("City", "City",Session::get("City")),
        new TextField("State", "State",Session::get("State")),
        new TextField("Zip", "Zip",Session::get("Zip")),
    new EmailField("Email", "Email address",Session::get("Email")),
        new PasswordField("Password", "Password")

    ), new FieldSet(
    // List the action buttons here
    new FormAction("doform", "push me")

    ), new RequiredFields(
    // List the required fields here: "Email", "FirstName"
             "FirstName","LastName","Zip","Email","Password"
    ));
          }
          else{
          
    return new Form($this, "Form", new FieldSet(
    // List the your fields here
    new TextField("FirstName", "First name"),
    new TextField("LastName", "Last Name"),
        new TextField("Address1", "Address 1"),
        new TextField("Address2", "Address 2"),
        new TextField("City", "City"),
        new TextField("State", "State"),
        new TextField("Zip", "Zip"),
    new EmailField("Email", "Email address"),
        new PasswordField("Password", "Password")

    ), new FieldSet(
    // List the action buttons here
    new FormAction("doform", "push me")

    ), new RequiredFields(
             "FirstName","LastName","Zip","Email","Password"
    ));
          }
    }
       
       

       function signups($email=null) {

           if(DataObject::get("myTable", "Email = '$email'"))
           return true;
           else
           return false;
       
       }

       function doform($data, $form) {
          if($this->signups($data["Email"]))
          {
          Session::set('RegistrationError', true);
          
          Session::set('FirstName', $data["FirstName"]);
          Session::set('LastName', $data["LastName"]);
          Session::set('Address1', $data["Address1"]);
          Session::set('Address2', $data["Address2"]);
          Session::set('City', $data["City"]);
          Session::set('State', $data["State"]);
          Session::set('Zip', $data["Zip"]);
          Session::set('Email', $data["Email"]);
          
          Director::redirectBack();
           }
          else
          {
          $submission = new myTable();
          $form->saveInto($submission);
          $submission->write();
          
          Director::redirectBack();
          }
       
       }

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: going back to form with populated fields Link to this post

    That does look too complicated for what you are actually trying to do. This is how I would be doing it

    class myForm_Controller extends Page_Controller {
    function Form() {
    $fields = new FieldSet(
    new TextField("FirstName", "First name"),
    new TextField("LastName", "Last Name"),
    new TextField("Address1", "Address 1"),
    new TextField("Address2", "Address 2"),
    new TextField("City"),
    new TextField("State"),
    new TextField("Zip"),
    new EmailField("Email", "Email address"),
    new PasswordField("Password")
    );

    $actions = new FieldSet(
    new FormAction("doform", "push me")
    );

    $required = new RequiredFields(array(
    'FirstName',
    'LastName',
    'Zip',
    'Password',
    'Email'
    ));

    $form = new Form($this, "Form", $fields, $actions, $required);

    if(is_array(Session::get('RegistrationForm')) {
    $form->loadDataFrom(Session::get('RegistrationForm');
    }
    return $form;
    }

    function signups($email=null) {

    if(DataObject::get("myTable", "Email = '$email'"))
    return true;
    else
    return false;

    }

    function doform($data, $form) {
    Session::set("RegistrationForm", $data);
    if($this->signups($data["Email"])) return Director::redirectBack();

    $submission = new myTable();
    $form->saveInto($submission);
    $submission->write();

    return Director::redirectBack();

    }

  • g
    Avatar
    Community Member
    22 Posts

    Re: going back to form with populated fields Link to this post

    Beautiful. That works lovely. Thanks.

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