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.

Form Questions

Passing data from first form to the second form


Reply

2 Posts   1948 Views

Avatar
px

12 March 2010 at 8:16pm (Last edited: 12 March 2010 8:35pm), Community Member, 10 Posts

hi! i would just like to ask if it's possible to create two forms. like for example, in my first form,i have a pros,cons and recommend room fields while on the second form i have name, email,often play category(drop down) and gender fields.what i want is on the first form,upon clicking the submit,it will go the 2nd form and that 2nd form will catch the data from the 1st form. upon completing all the fields, that's the time that it'll be saved.

for example, here is my first method:

function UserReviewsCommentFormStepOne() {
      // Create fields
      $fields = new FieldSet(
         new TextareaField("Pros","What did you like about this room?"),
         new TextareaField("Cons","What did you not like in this room?"),
         new OptionsetField("RecommendRoom",'Do you recommend this room?',array(
                  'Y'=>'Yes',
                  'N'=>'No'
               )));
      
      $actions = new FieldSet(new FormAction('UserReviewsCommentFormStepTwo', 'Submit'));
      $validator=new RequiredFields('Pros','Cons','RecommendRoom');
      $form = new Form($this, 'UserReviewsCommentFormStepTwo', $fields, $actions,$validator);
return $form;
   }

and here is my second form that will catch the data from the first form:

function UserReviewsCommentFormStepTwo($data) {

      if($data) {
         $fields = new FieldSet(
         new OptionsetField("Gender",'Sex',array(
                  'M'=>'Male',
                  'F'=>'Female'
               )),
         new DropdownField("OftenPlayCategory",'The Categories: ',$this->getOftenPlayCategory(),$this->val( 'ID' ),null,true),
         new TextField("Name","Your Name"),
         new TextField("Email","Your email address"),
         new HiddenField("Pros","Pros: ",$data['Pros']),
         new HiddenField("Cons","Cons: ",$data['Cons']),
         new HiddenField("RecommendRoom","Recommend Room: ",$data['RecommendRoom'])
         );
         $actions = new FieldSet(new FormAction('ProcessUserReviewsForm', 'Submit'));
         $validator=new RequiredFields();
         $form = new Form($this, 'ProcessUserReviewsForm', $fields, $actions, $validator);
      }

      
return $form;

   }

upon doing this(running this), i get this error:
[User Error] Uncaught Exception: Object->__call(): the method 'getviewer' does not exist on 'Form'
POST /UserReviewsWidget_Controller/UserReviewsCommentFormStepTwo

can anyone help me on how to solve this question of mine?thanks :)

Avatar
Willr

13 March 2010 at 2:42pm (Last edited: 13 March 2010 2:47pm), Forum Moderator, 5511 Posts

In your UserReviewsCommentFormStepTwo since this is a controller action you need to return an array which passes the new form back to the template.

function UserReviewsCommentFormStepTwo($data, $form) {
// ...

$form = new Form(...
$form->loadDataFrom($data);
return $this->customize('Form' => $form);
}

The other thing you could do is have a seperate action on your controller then you pass the form data via the session

function doUserReviewsCommentFormStepOne($data, $form) {
Session::set('FormData', $data);

$this->redirect('steptwo');
}

Then you would have a template ClassName_steptwo.ss with the other form. Or another option is to checkout the MultiStep Form module. Which is a pretty good module. Supports going back / forward etc. Might be overkill for 2 pages but it does provide a nice solution