views:

46

answers:

2

This is a pretty basic thing but I can't figure out how to solve this "properly" with Zend Framework:

Scenario:

  1. Page displays form 1,
  2. Page display form 2

This is a pretty basic thing but I can't figure out how to solve this "properly" with Zend Framework:

Scenario:

  1. Page displays form 1,
  2. Page displays form 2
class FooController extends Zend_Controller_Action {  
    ...  
    public function form1Action(){  
        if ($this->getRequest()->isPost()) {  
           // save data from form1 in database  
           $this->_forward('form2');  
        }  
        // display form1  
    }  
    public function form2Action(){  
        if ($this->getRequest()->isPost()) {  
           // save data from form2 in database  
           $this->_forward('somewherelese');  
        }  
        // display form2  
    }  
}  

When the user posts form1, first the if-condition in form1Action is executed (which is what I want), but also the if-condition in form2Action.

What would be toe proper way to "unset $this->getRequest()->isPost()"?

Note: the forms are build "by hand" (not using Zend Form)

+4  A: 

You have three options:

  1. Use _redirect instead of _forward. Forward redirects under the same request. Redirect will create a new request.'
  2. Set a param in your _forward call, which you can check for in your second form: Such as 'form' => 2. More information.
  3. Use the built in multipage forms that are included in Zend_Form out of the box.
balupton
That's how I solved it for the time being, but I am not satisfied with this solution as creating a new request means more stress for the server. I would like to avoid this.. :-)
jamie0725
I've updated my answer to incude a new option. Option #2 which will provide a solution for you if you still want to use _forward.
balupton
A: 

You could always set a class variable in action one and if it is true, don't run the code in action two.

Something like:

class FooController extends Zend_Controller_Action {  
private $_fromAction1 = false;
    ...  
    public function form1Action(){  
        if ($this->getRequest()->isPost()) {  
           // save data from form1 in database  
$this->_fromAction1 = true;
           $this->_forward('form2');  
        }  
        // display form1  
    }  
    public function form2Action(){  
        if ($this->getRequest()->isPost() && !$this->_formAction1) {  
           // save data from form2 in database  
           $this->_forward('somewherelese');  
        }  
        // display form2  
    }  
} 
Ashley