Tomek Kobyliński
Tomek Kobyliński

Reputation: 1308

Multipage form in Zend Framework 2

I want to make a form divided into two steps - each on a separate page. Depending on the data given in the first step, i want to prepare some prefilled data in the second step.

Despite spending a lot of time searching in google I can not find it.

From a logical point of view, I would prefer one Zend\Form and separate fieldsets (??) than create two separate forms.

So how to do it correctly in Zend Framework 2?

Upvotes: 2

Views: 880

Answers (1)

Jurian Sluiman
Jurian Sluiman

Reputation: 13558

The easiest way would (I think) to have two forms, a MyModule\Form\Step1 and a MyModule\Form\Step2. The Step2 form accepts some input in the constructor which is depending on some data from step 1.

An example, you can do this with the service manager's factory:

'factories' => array(
  'MyModule\Form\Step1' => function($sl) {
    return new MyModule\Form\Step1;
  },
  'MyModule\Form\Step2' => function($sl) {
    $request = $sl->get('Request');
    $data    = $request->getPost();
    return new MyModule\Form\Step2($data);
  }
),

If you GET the page where the first one is displayed, then you POST to the second page and finally POST to the last page. During construction of the second form, you use the POST data and populate/construct the data as needed.

Upvotes: 2

Related Questions