Zend Action viewRenderer() is POST-ing my form

61 views Asked by At

I am trying to send a parameter from the indexAction to the editAction using the viewRender function. The problem is when the editAction is called it causes my $form to think it has been posted.

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 POSTED" is printed immediately even though I have not posted the form yet. I'm not sure why the form $form->posted is set to true on the initial render. Does anyone have an idea of why this is or a work around?

2

There are 2 answers

1
Tim On

You should check your form like this:

$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;
0
Alexandre Brach On

I'm not sure about what you want to obtain, but in order to communicate value between two action, it should be better to use _getParam and _setParam method :

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');
    }
    ...
}