2013-04-07 60 views
1

我有一个用于创建新“组”的表单。我现在添加了一个小的“返回”图像,用户应该能够返回一步。我不知道为什么,但是当我点击这个新图像时,用于我想要离开的表单的控制器和动作(/admin/creategroup)被再次用HTTP POST集进行调用。因此,表单验证已完成,并且我坚持显示验证错误的此表单。Zend:从未经过验证的表单重定向

这是我的表单中带有两个图像按钮的代码片段。我wan't的“返回” -image我重定向到指定控制器不用验证表单

$this->addElement('image', 'btnBack', array (
     'name' => 'btnBack', 
     'id' => 'btnBack', 
     'label' => '', 
     'title' => 'Go back', 
     'alt' => 'Go back', 
     'src' => '/img/undo.png', 
     'onClick' => "window.location='/admin/groupoverview'" 
    )); 

    $this->addElement('image', 'btnSave', array (
     'name' => 'btnSave', 
     'id' => 'btnSave', 
     'label' => '', 
     'title' => 'Save this new group', 
     'alt' => 'Save this new group', 
     'src' => '/img/save.png', 
     'onClick' => "document.forms[0].submit();" 
    )); 

编辑:
我已经想到的可能性在/管理/ creategroup检查无论是从'btnBack'图像还是'btnSave'图像调用,并且如果源是'btnBack'图像,都跳过表单验证并正确重定向。
我只是认为应该有一个更好的解决方案,直接从窗体重定向,并规避再次调用/admin/creategroup

EDIT2:
视图脚本:

public function creategroupAction() 
{ 
    $form = new Application_Form_CreateGroup(); 
    $request = $this->getRequest(); 

    if ($request->isPost()) { 
     if ($form->isValid($request->getPost())) { 
      // Data for new group is valid 
      ... 
     } else { 
      // Form data was invalid 
      // => This is where I land when pressing the 'back' image 
      // No further code here 
     } 
    } 
    $this->view->form = $form; 
} 
+0

没关系doneeeee – RockyFord 2013-04-08 04:21:26

+0

@RockyFord:原谅我?你的评论似乎表明,人们只是看一眼这个问题,我的问题已经解决了,情况并非如此。我仍然在寻找这个解决方案。 – Daniel 2013-04-11 17:59:08

回答

0

现在有一些工作:

<div id="createGroupMask"> 
    <br/> 
    Use the form below to create a new group 
    <?php 
     $this->form->setAction($this->url()); 
     echo $this->form; 
    ?> 
</div> 

控制器我行动

和isValid ()循环不正确,你的表单将永远不会评估如同inValid关于你所呈现的元素一样,你将永远不会去其他人。

public function creategroupAction() 
{ 
    $form = new Application_Form_CreateGroup(); 
    $request = $this->getRequest(); 

    if ($request->isPost()) { 
     if ($form->isValid($request->getPost())) { 
      // Data for new group is valid 
      ... 
     } else { 
/* This is incorrect */ 
      // Form data was invalid 
      // => This is where I land when pressing the 'back' image 
      // No further code here 
     } 
    } 
    $this->view->form = $form; 
} 

我的问题是,我不知道是怎么回事从您的表单提交,我不是很熟悉如何使用您的“点击”,什么我相信是的JavaScript。它看起来像元素btnBack应该在点击元素btnSave应该POST重定向。但是,这似乎并没有发生。

我已经做过这类事情在PHP和ZF与提交按钮,也许我所做的流量会有所帮助:

注:此类型的流动工作,你必须给按钮元素的标签。该标签用作submit value

//psuedoCode 
public function creategroupAction() 
{ 
    $form = new Application_Form_CreateGroup(); 
    $request = $this->getRequest(); 

    if ($request->isPost()) { 
     if ($form->isValid($request->getPost())) { 
      //I would probably opt to perform this task with a switch loop 
      if ($form->getValue('btnBack') === some true value) { 
       $this->_redirect('new url'); 
      } 
      if ($form->getValue('btnSave') === some true value) { 
       //Process and save data 
      } 

     } else { 
      //Display form errors 
    } 
    $this->view->form = $form; 
} 

我认为,当一切都说过和做过你的问题的关键是,你没有给你的按钮元素的标签。

0

我试着给我的图像添加标签,但这没有奏效。

我也试过用isChecked()方法对我btnBack像这样的:

if ($form->btnBack->isChecked()) { 
    // 'Go back' image was clicked so this is no real error, just redirect 
} 

这也不能工作。

我终于能够检查通过以下方法进行点击的图像作为回答Zend form: image as submit button

public function creategroupAction() 
{ 
    $form = new Application_Form_CreateGroup(); 
    $request = $this->getRequest(); 

    if ($request->isPost()) { 
     if ($form->isValid($request->getPost())) { 
      // Data for new group is valid 
      ... 
     } else { 
      // Form data was invalid 

      if (isset($this->_request->btnBack_x)) { 
       // 'Go back' image was pressed, so this is no error 
       // -> redirect to group overview page 
       $this->_redirect('/admin/groupoverview'); 
      } 
     } 
    } 
    $this->view->form = $form; 
} 

我想这并没有彻底回答原来的问题作为验证仍在进行,我m只检查点击“返回”图像的'特殊情况',但我会将其标记为无论如何应答。