代码之家  ›  专栏  ›  技术社区  ›  Jeremy Hicks

Zend Framework:发布到不同的操作,如果验证失败则返回到原始操作并保留表单字段

  •  2
  • Jeremy Hicks  · 技术社区  · 16 年前

    这听起来可能有点奇怪,但我在一页上有两张表格。一个只是发回自己。我对另一个动作做了第二个帖子,以保持代码更干净。也许不是正确的选择。。。

    我现在遇到的问题是,如果第二个表单没有验证,我会重定向回包含表单的页面,但我不知道如何用用户输入的原始信息填充表单字段。有没有办法做到这一点,并不断张贴到两个独立的行动,还是我需要咬紧牙关,让两个形式张贴回同一个行动,并处理混乱的逻辑?

    4 回复  |  直到 15 年前
        1
  •  3
  •   Lee    16 年前

    我会把两份表格提交给同一个行动。这件事真的不应该太麻烦。在每个表单中都包含一个隐藏字段,表示要提交哪个表单。

    申请表登录:

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

    申请表登记:

     /* 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() 将保留已输入的任何数据,并且在成功验证一个或两个表单时仍将重定向。

        2
  •  1
  •   Community Mohan Dere    9 年前

    我个人认为 应该 就像你所拥有的,贴到它自己的控制器上。这会将处理该表单的代码保存在一个地方。这里的问题是,您希望返回到验证失败的原始页。但为什么呢?为什么不简单地在目标控制器中重新显示表单,就像页面上只有一个表单一样?

    例如,考虑一个出现在sie的每一页上的登录表单(可能是因为它在站点模板/布局中)。它发布到 AuthController::loginAction() . 如果登录失败,则通常不会将他发送回他来自的页面。你把他留在登录页面,表格由 $_POST 就像你想要的那样(可能是用户名,但不是他的密码)。

    见 this answer 进行类似的讨论。

    更新 :在这方面有另一个想法。如果您真的想在两个不同的控制器中处理该处理,以便让他保持在发布表单的页面上,那么至少将该表单处理提取到 action helper . 这样,您至少可以保持表单处理 DRY .

    更新 :Rob Allen刚刚写了一篇很棒的博文 "A form in your layout" 其中,他描述了一个方法,该方法使用带有 preDispatch() 方法来实例化和处理窗体。很不错的。

        3
  •  0
  •   BlueDog    16 年前

    如何重定向?如果你再次显示表单页面,我看不出问题。可以使用Zend_Form::populate()预先填充表单。

        4
  •  0
  •   Richard Knop    16 年前

    好吧,我会把两份表格放在同一页上。

    我不明白为什么你的代码会变得更不可读。学习如何使用动作助手,你的控制器会突然变得非常简单易读:

    public function indexAction() 
    {
        $request = $this->getRequest();
    
        // send forms to view, so we can print them
        // but also so we can access them in action helpers
        $this->view->form = $this->_getForm('Form1', '/');
        $this->view->form2 = $this->_getForm('Form2', '/');
    
        if ($request->isPost())
        {
            // process the first form
            if (isset($_POST['form_submit_button']) && $this->view->form->isValid($_POST))
            {
                $this->_helper->form($this->view->form->getValues());
            }
            // process the second form
            else if (isset($_POST['form2_submit_button']) && $this->view->form2->isValid($_POST))
            {
                $this->_helper->form2($this->view->form2->getValues());
            }
    }
    

    每个表单的处理都有自己的动作助手。