Zend Action viewRenderer()正在POST我的表单


Zend Action viewRenderer() is POST-ing my form

我正试图使用viewRender函数将参数从indexAction发送到editAction。问题是,当调用editAction时,它会导致我的$form认为它已经发布。

public funciton indexAction(){
    ...
    if(isset($_POST['edit'])){
       $this->_helper->viewRenderer('edit');
       $this->editAction($thingINeed);
    }
    ...
}
public function editAction($thingINeed){
    ...
    if($form->posted){
        var_dump('FORM POSTED');
    }
    ...
}

即使我还没有张贴表格,"表格张贴"也会立即打印出来。我不知道为什么发布的表单$form->在初始渲染时被设置为true。有人知道为什么会这样吗?

您应该这样检查您的表单:

$form = new MyForm();
if ($this->_request->isPost()) {
    $formData = $this->_request->getPost();
    if ($form->isValid($formData)) {
        echo 'success';
        exit;
    } else {
        $form->populate($formData);
    }
}
$this->view->form = $form;

我不确定您想要获得什么,但为了在两个操作之间传递值,最好使用_getParam和_setParam方法:

public funciton indexAction(){
    ...
    if(isset($_POST['edit'])){
        $this->_setParam( 'posted', true );
        $this->_helper->viewRenderer('edit');
        //$this->editAction($thingINeed);       
        // It should be better to use Action stack helper to route correctly your action :
        Zend_Controller_Action_HelperBroker::getStaticHelper( 'actionStack' )->actionToStack( 'edit' );
    } else {
        $this->_setParam( 'posted', false );
    }
    ...
}
// param $thingINeed is not "needed" anymore
public function editAction(){
    ...
    if( true == $this->_getParam( 'posted' ) {
        var_dump('FORM POSTED');
    }
    ...
}