Zend Framework:发布到不同的操作,如果验证失败并保留表单字段,则返回到原始操作
这听起来可能很奇怪,但我在一页上有两个表格。一个只是发回给自己。我在另一个动作中发布了第二篇文章,以保持代码简洁。也许不是正确的选择...
我现在遇到的问题是,如果第二个表单未验证,我将重定向回包含该表单的页面,但我不知道如何让我的表单字段填写为用户输入的原始信息。有没有办法做到这一点并继续发布到两个单独的操作,或者我是否需要硬着头皮将两个表单发布回同一个操作并处理混乱的逻辑?
This might sound like an odd scenario, but I've got two forms on one page. One is just posting back to itself. I made the second post to another action to keep the code cleaner. Maybe not the right choice...
The problem I'm having now is that if that second form doesn't validate, I redirect back to the page with the form but I don't know how to keep my form fields filled in with the original information the user entered. Is there a way to do that and keep posting to two separate actions, or do I need to just bite the bullet and have both forms post back to the same action and deal with the messy logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会将这两种表格提交给同一个操作。其实不应该有什么太乱的事情。每个表单中都包含一个隐藏字段来表示正在提交哪个表单。
Application_Form_Login:
Application_Form_Register:
Controller:
View:
使用这种类型的设置,如果您的任何一个表单验证失败,
isValid()
将保留已输入的所有数据,并且您仍然可以在成功验证时重定向一种或两种形式。I would submit both forms to the same action. There really shouldn't be anything too messy about it. In each form include a hidden field to signify which form is being submitted.
Application_Form_Login:
Application_Form_Register:
Controller:
View:
With this type of a setup, if either of your forms fail validation,
isValid()
will preserve any data that has been entered and you still redirect on a successful validation of one or both of the forms.就我个人而言,我认为每个表单应该发布到自己的控制器,就像您一样。这将处理该表单的代码保留在一个位置。这里的问题是您希望在验证失败时返回原始页面。但为什么?为什么不简单地在目标控制器中重新显示表单,就像页面上只有一个表单一样?
例如,考虑一个出现在 sie 的每个页面上的登录表单(可能是因为它在站点模板/布局中)。它会发布类似
AuthController::loginAction()
的内容。如果登录失败,那么您通常不会将他发送回他来自的页面。您将他留在登录页面,并根据您的需要从$_POST
中预先填写了表单(可能是用户名,但不是他的密码)。有关类似的讨论,请参阅此答案。
更新:对此有另一个想法。如果您确实想在两个不同的控制器中处理处理,以便让他留在发布表单的页面上,至少将该表单处理提取到 动作助手。这样,您至少可以保持表单处理干燥。
更新:Rob Allen 刚刚写了一篇很棒的博客文章 “布局中的表单”,其中他描述了一种方法,该方法使用带有
preDispatch()
方法的操作帮助程序来实例化和处理表单。很不错。Personally, I think that each form should post to its own controller, as you have. This keeps the code for processing that form in a single place. The issue here is that you want to return to the original page on failed validation. But why? Why not simply redisplay the form in the target controller, just like you would if there were a single form on the page?
For example, consider a login form that appears on every page of a sie (perhaps because it in the site template/layout). It posts to something like
AuthController::loginAction()
. If the login fails, then you don't typically send him back to the page from which he came. You leave him at the login page, with the form as pre-filled from the$_POST
as you want it to be (probably a username, but not his password).See this answer for a similar discussion.
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. This way, you could at least keep that form-processing DRY.
Update: Rob Allen has just written a great blog post "A form in your layout" in which he describes a method that uses an action-helper with a
preDispatch()
method that instantiates and processes the form. Very nice.你如何重定向?如果您再次显示表单页面,我就看不到问题了。您可以使用 Zend_Form::populate() 预填充表单。
How do you redirect? I don't see the problem if you just display the form page again. You can prefill you forms using Zend_Form::populate().
好吧,我只是将两个表单都提交在同一页面上。
我不明白为什么你的代码可读性会降低。了解如何使用操作助手,您的控制器会突然看起来非常简单和可读:
每个表单的处理都有自己的操作助手。
Well, I would just keep both forms submitting on the same page.
I don't see why your code should get any less readable. Learn how to use action helpers and your controllers will suddenly look extremely simple and readable:
Each form's processing would have its own action helper.