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.

Archive /

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

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

going back to form with populated fields


Reply


5 Posts   2331 Views

Avatar
g

Community Member, 22 Posts

28 October 2008 at 9:39am

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?

Avatar
Willr

Forum Moderator, 5513 Posts

28 October 2008 at 9:05pm

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

Avatar
g

Community Member, 22 Posts

29 October 2008 at 11:36am

Edited: 29/10/2008 11:37am

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

Avatar
Willr

Forum Moderator, 5513 Posts

29 October 2008 at 11:48am

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();

}

Avatar
g

Community Member, 22 Posts

30 October 2008 at 3:52am

Beautiful. That works lovely. Thanks.