Persist Zend Form object after redirect

2019-09-01 15:04发布

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?

4条回答
何必那么认真
2楼-- · 2019-09-01 15:12

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?

查看更多
Summer. ? 凉城
3楼-- · 2019-09-01 15:23

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:

  1. On page B after unsuccessful validation store validation messages in session.
  2. Redirect to page A.
  3. Before rendering form, check if there are messages in session, if they are, set them to form.
  4. Render form - if message are set, they will be generated by Errors decorator.
查看更多
迷人小祖宗
4楼-- · 2019-09-01 15:36

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.

查看更多
Ridiculous、
5楼-- · 2019-09-01 15:37

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.

查看更多
登录 后发表回答