How to implement a form preview page in Zend Framework 2?

114 Views Asked by At

In my current ZF2 project I have a complex Form with multiple levels of nested Fieldsets, that reflect the structure of objects to be saved in the background. Currently the data is sent directly to the Controller and gets saved to the database, if it's valid.

Now an intermediate step should be implemented: The user should get a chance to check the input data before it's saved to the database. If he decides, that it's correct, the form data should be submitted and saved to the database; if the user decides, that the form has to be edited, he should be able to go back to the form and correct it. (Of course all that in a loop, until the user is happy with the form and submit it.)

That means, a preview page is needed. This page/action should get the data and display it somehow (as a table or however). The data needs to be stored somewhere temporarily and be ready to be "hydrated" to the Form object and saved. If the user wants to update the form, the form should be restored.

How can I implement this requirement?

UPDATE

I'm looking for a server-side solution. The preview should be a new page and not a JavScript/client-side generated HTML on the same page (for the tracking and other purposes).

2

There are 2 best solutions below

4
Wilt On

I would say that the best solution would be to implement a client sided (java-)script that is executed before your perform the form POST request. You could catch the form submit event and render a client side view in which you show the current state of all the form fields (name and value). If the user clicks accept you continue the POST operation. If the user clicks cancel you return to the view of the form where you allow additional changes and the whole thing repeats itself.

It should not be difficult to find examples on how to do this...

One example is this blog post, but there are many more to find with the help of Google.

0
metalinspired On

Since I don't know how your application/db is structured I can only speculate that you could create another table that would hold temporary data or after the user submits the form and it is validated instead of saving it to db make all fields read-only and replace original submit button with the one that will save the data and the one that will take user back to the form where he/she can change the data.