Zend_Form - several forms on one page

Having multiple forms on one page, when I submit one of them, how can I tell which one was submitted?

I thought about creating uniqe identifiers for each of them and saved them as hidden fields and user session - although this is a solution, the problem is that there is no good place to remove old identifiers from the session.

Any best ideas on how to solve this?

Thanks in advance!

+1
source share
2 answers

First of all: do you consider sending two forms to two different actions? Thus, you can process each form separately in each action. This should be the "best price" if you are using the Zend MVC component.

Another option is to check the value of the submit button that will be included in the request, for example

<input type="submit" name="save" value="form1" /> // in PHP: // $_POST["save"] will contain "form1" <input type="submit" name="save" value="form2" /> // in PHP: // $_POST["save"] will contain "form2" 

Be careful, as the value symbol will be displayed as a button label.

So maybe you want to distingush the form using different submit button names:

 <input type="submit" name="save-form1" value="Submit" /> // in PHP: // $_POST["save-form1"] will contain "Submit" <input type="submit" name="save-form2" value="Submit" /> // in PHP: // $_POST["save-form2"] will contain "Submit" 

EDIT:

During the comment dialog between OP and me, the following seems to be a possible solution:

 class My_Form_Base extends Zend_Form { private static $_instanceCounter = 0; public function __construct($options = null) { parent:: __construct($options); self::$_instanceCounter++; $this->addElement('hidden', 'form-id', sprintf('form-%s-instance-%d', $this->_getFormType(), self::$_instanceCounter); } protected _getFormType() { return get_class($this); } } class My_Form_Type1 extends My_Form_Base { public function init() { // more form initialization } } class My_Form_Type2 extends My_Form_Base { public function init() { // more form initialization } } 
+3
source

some errors in your code, shoudl will be something like this:

 class Application_Form_Idee_Base extends Zend_Form { private static $_instanceCounter = 0; public function __construct($options = null) { parent::__construct($options); self::$_instanceCounter++; $this->addElement('hidden', 'form-id', array( 'value' => sprintf('form-%s-instance-%s', $this->_getFormType(), self::$_instanceCounter)) ); } protected function _getFormType() { return get_class($this); } } 
0
source

All Articles