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

查看:52
本文介绍了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?

推荐答案

我会将两种形式都提交给同一操作.真的不应该对此太杂乱.在每个表单中都包含一个隐藏字段,以指示要提交的表单.

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 Framework:如果验证失败并保留表单字段,则发布到其他操作,然后返回到原始操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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