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

2

6

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

Athletics answered 5/5, 2016 at 16:3 Comment(2)
Can't you serialize the objects and store them in a session. Then un-serailize the objects to save them to the database after the user has accepted their data.Fattish
What object are you meaning? Form? And where to store -- in the session?Athletics
O
0

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.

Ordinand answered 16/5, 2016 at 14:44 Comment(4)
Thank you for your answer! Well, theoretically it would work, but with this approach I would need to duplicate all the server side implemented validation logic reimplemening it in JavaScript.Athletics
You wrote: "The user should get a chance to check the input data before it's sent to the server; if he decides, it's correct, the form should be submitted and saved to the database", and that is possible without adding any validation: 1) the user checks his input. 2) when the user thinks all is fine you can still let the server do its job and validate. But if you want the server to validate before the user you could indeed write similar code for validation on the client. It would be more efficient then letting the server do all the work on every attempt. Html5 validation can be helpful.Ordinand
@Athletics There are some modules for ZF2 that can help you integrate client side validation in your project. I have no first hand experience with any, but it might be worth a try.Ordinand
The question was not precise enough. Please see the update. ThxAthletics
H
0

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.

Hyaluronidase answered 28/5, 2016 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.