【问题标题】:Zend form: how to restore proper view after validation failure?Zend 表单:验证失败后如何恢复正确的视图?
【发布时间】:2012-07-26 07:57:44
【问题描述】:

我在使用 Zend 表单验证失败后重新显示视图时遇到问题。

我的初始操作看起来像

public function test1Action() {
    // get a form and pass it to the view
    $this->view->form = $this->getForm(); 

    // extra stuff I need to display 
    $this->view->name = "Bob";
}

还有风景

Hello <?= $this->name?>
<?php echo $this->form;?>

当表单提交后调用的动作“test2”出现验证错误时,问题就来了:

public function test2Action() {
    if (!$this->getRequest()->isPost()) {
        return $this->_forward('test1');
    }

    $form = $this->getForm();
    if (!$form->isValid($_POST)) {
        $this->view->form = $form;
        return $this->render('test1'); // go back to test1
    }
}

确实,变量“name”丢失了,视图不正确。而不是说“你好鲍勃”,而是说“你好”。

我应该如何处理?我应该重定向到 test1 而不是再次渲染 test1 吗?如何 ?

编辑

根据 Coder 先生的回答,我得到了以下结果:

控制器:

    public function getForm() {
        // what is important is that the form goes to action test3 and not test4
        // SNIP form creation with a field username
        return $form;
    }

    public function test3Action()
    {
        $this->view->form = $this->getForm();
        $this->view->name = "Bob";

        if(!$this->getRequest()->isPost())return;

        if($this->view->form->isValid($_POST))
        {
            //save the data and redirect
            $values = $this->view->form->getValues();
            $username = $values["username"];

            $defaultSession = new Zend_Session_Namespace('asdf');
            $defaultSession->username = $username;

            $this->_helper->redirector('test4');
        }
    }


    public function test4Action()
    {
        $defaultSession = new Zend_Session_Namespace('asdf');
        $this->view->username = $defaultSession->username;
    }

test3.phtml

Hello <?= $this->name?>
<?php echo $this->form;?>

test4.phtml

success for <?php echo $this->username;?>

【问题讨论】:

    标签: forms validation zend-framework


    【解决方案1】:

    把你的鲍勃带回来做

    public function test2Action() {
        if (!$this->getRequest()->isPost()) {
            return $this->_forward('test1');
        }
    
        $form = $this->getForm();
        if (!$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->view->name = 'bob';        
            return $this->render('test1'); // go back to test1
        }
    }
    

    但我会建议我处理表单的解决方案,类似于

    public function test2Action()
    {
    
       $this->view->form = new My_Form();
    
       if(!$this->getRequest()->isPost())return;   
    
      if($this->view->form->isValid())
       {
        //save the data and redirect 
           $this->_helper->redirector('success');
       }
    
      }
    

    在你的 test2.phtml 中

    <?php echo $this->form ?>
    

    }

    这种方法使您无需创建多个操作来保存一个表单并手动更改视图。

    【讨论】:

    • “把你的鲍勃带回来做”:谢谢你让我发笑:) 我不确定完全理解你处理表格的方式。您是否摆脱了 test1 并只保留 test2 ?
    • 你能检查我在问题中的编辑,看看它是否是你的想法吗?谢谢
    • 是的,让它变得更好(成功消息处理)使用 ZF flashmessenger action helper framework.zend.com/manual/en/…
    猜你喜欢
    • 2021-12-15
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 2013-07-25
    • 2015-07-19
    相关资源
    最近更新 更多