Jump to:

1971 Posts in 1275 Topics by 607 members

Form Questions

SilverStripe Forums » Form Questions » Passing data from first form to the second form

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

Page: 1
Go to End
Author Topic: 1822 Views
  • px
    Avatar
    Community Member
    10 Posts

    Passing data from first form to the second form Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Passing data from first form to the second form Link to this post

    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

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