1
votes

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)

3
Shouldn't you be using isValid() at some point? This should stop you from processing the data in form2 when form1 is POSTed, unless of course they are exactly the same. Or use Zend's built in Multi-Page Forms documented here framework.zend.com/manual/en/zend.form.advanced.htmlJake N
Makes perfectly sense, but in this case the forms are build "by hand", i.e. not using Zend_Form...jamie0725

3 Answers

4
votes

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.
0
votes

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  
    }  
} 
0
votes

This last option did not work for me.

$this->_forward() creates a new instance of the controller, so setting a variable in the first instance does not affect the one in the new.

My solution was making $_fromAction1 static to share the variable between the 2 instances.

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