So the thing is I have a form in page A which is being submitted at page B.
After validating the form at page B, it should redirect back to page A and show the form with the possible errors that occurred.
What's the best way to achieve this? Is it smart to temporary put the whole form object in the session? Or is there a more elegant solution?
Why not validate it on page A, and then forward to page B if it's valid? That way you don't have to do any ugly hacks.
The correct way is to submit the form to the same page, and only if it's valid you redirect via a Location:
header to the next page.
It sounds to me like Page A is the culprit here.
Rather than rendering the login form on Page A when the user is not logged in, you could save the url for Page A in the session, then redirect to the (sole!) login page (Page B, right?), handled by something like AuthController::loginAction()
.
On failed validation, you keep him there on the login page, displaying the validation errors and the form. On successful validation/login, you check the session to see if there is a saved url there. If so, send him there. If not, send him to some place of your choosing, either his profile page or the home page or some kind of a congrats-you-are-logged-in page.
See what I mean?
Zend_Form
has methods like getMessages
, getErrorMessages
and setErrors
, setErrorMessages
and so on. You can try do it that way - never tried, but theoreticaly should work:
- On page B after unsuccessful validation store validation messages in session.
- Redirect to page A.
- Before rendering form, check if there are messages in session, if they are, set them to form.
- Render form - if message are set, they will be generated by
Errors
decorator.