Zend 框架:如果验证失败并保留表单字段,则发布到不同的操作然后返回到原始操作 [英] Zend Framework: Post to different action then return to original action if fails validation AND keep form fields

查看:23
本文介绍了Zend 框架:如果验证失败并保留表单字段,则发布到不同的操作然后返回到原始操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来可能有点奇怪,但我在一页上有两个表格.一种是回贴给自己.我将第二个帖子用于另一个操作以保持代码更清晰.也许不是正确的选择...

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?

推荐答案

我会将两个表单提交给同一个操作.真的不应该有什么太乱了.在每个表单中包含一个隐藏字段以表示正在提交哪个表单.

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_Login:

 /* other form elements */
 $this->addElement('hidden', 'login', array(
      'value' => 1
 ));

Application_Form_Register:

Application_Form_Register:

 /* other form elements */
 $this->addElement('hidden', 'register', array(
      'value' => 1
 ));

控制器:

$loginForm = new Application_Form_Login();
$registerForm = new Application_Form_Register();

if($this->_request->isPost()) {
     if($this->_request->getPost('login')) {
         if($loginForm->isValid($this->_request->getPost())) {
             // validated, redirect
             $this->_helper->redirector('profile', 'user');
         }
     }
     if($this->_request->getPost('register')) {
         if($registerForm->isValid($this->_request->getPost())) {
             // validated, proceed as needed
         }
     }
}

$this->view->loginForm = $loginForm;
$this->view->registerForm = $registerForm;

查看:

echo $this->loginForm;

echo $this->registerForm;

使用这种类型的设置,如果您的任一表单未通过验证,isValid() 将保留已输入的所有数据,并且您仍会重定向其中一个或两个表单的成功验证表格.

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.

这篇关于Zend 框架:如果验证失败并保留表单字段,则发布到不同的操作然后返回到原始操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆