views:

28

answers:

3

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?

A: 

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.

cweiske
+1  A: 

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.

reko_t
Page A is a page which contains functionality which isn't available when not logged in so the login form is embedded on that page. The actual validation of that form is handled on page B though. It kinda seems wrong to me to have 2 actions which handle the same form.
sander
A: 

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?

David Weinraub
I know what you mean but I don't really want the user to leave that page if the validation fails. The user might not understand it.
sander
It's a pretty common workflow: "Sorry, Login is required first." followed by a redirect to the login page; I suspect the user would understand. But if you must display the form on page A, even when validation fails, then I think you are stuck with the approaches you already noted. But they both feel a bit clunky to me, requiring controller checks for either session vars or for form handling. If you do form handling in two actions, then you could at least use an action helper to keep that code somewhat [DRY](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself).
David Weinraub
**Update**: Had another thought in this. If you really want to handle the processing in two different controllers in order to keep him on the page from which he posted the form, at least extract that form processing out into an [action helper](http://framework.zend.com/manual/en/zend.controller.actionhelpers.html). This way, you could at least keep that form-processing [DRY](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself). See [similar discussion](http://stackoverflow.com/questions/3876877/zend-framework-post-to-different-action-then-return-to-original-action-if-fails/3878814#3878814).
David Weinraub