Note that there are some explanatory texts on larger screens.

plurals
  1. PORendering and process same form twice in Zend Framework 2?
    text
    copied!<p>I have a requirement of rendering and processing the same form again if user check a select box. I looked into form collections but it didn't exactly access my problem because I don't need to render a set of fields instead my requirement is to render the complete form again. So what I added another function getClone($prefix) to get the clone of form which returns me the clone of form object after adding a prefix in the name of form. Like this</p> <pre><code>&lt;?php namespace Company\Form; use Zend\Form\Form; use Zend\InputFilter\Factory as InputFactory; use Zend\InputFilter\InputFilter; use Zend\InputFilter\InputFilterAwareInterface; use Zend\InputFilter\InputFilterInterface; class CompanyAddress extends Form implements InputFilterAwareInterface { protected $inputFilter; public function __construct($countries, $name = 'null') { parent::__construct('company_address'); $this-&gt;setAttribute('method', 'post'); $this-&gt;setAttribute('id', 'company_address'); $this-&gt;add(array( 'name' =&gt; 'street_1', 'attributes' =&gt; array( 'id' =&gt; 'street_1', 'type' =&gt; 'text', ), 'options' =&gt; array( 'label' =&gt; 'Street *', ) )); $this-&gt;add(array( 'name' =&gt; 'street_2', 'attributes' =&gt; array( 'id' =&gt; 'street_2', 'type' =&gt; 'text', ), 'options' =&gt; array( 'label' =&gt; 'Street 2', ) )); $this-&gt;add(array( 'name' =&gt; 'country_id', 'type' =&gt; 'Zend\Form\Element\Select', 'attributes' =&gt; array( 'id' =&gt; 'country_id', ), 'options' =&gt; array( 'label' =&gt; 'Country', 'value_options' =&gt; $countries ) )); $this-&gt;add(array( 'name' =&gt; 'city_id', 'type' =&gt; 'Zend\Form\Element\Select', 'attributes' =&gt; array( 'id' =&gt; 'city_id', ), 'options' =&gt; array( 'label' =&gt; 'City ', 'value_options' =&gt; array() ) )); $this-&gt;add(array( 'name' =&gt; 'zip', 'attributes' =&gt; array( 'id' =&gt; 'zip', 'type' =&gt; 'text', ), 'options' =&gt; array( 'label' =&gt; 'ZIP', 'value_options' =&gt; array() ) )); $this-&gt;add(array( 'name' =&gt; 'address_type', 'type' =&gt; 'Zend\Form\Element\Select', 'attributes' =&gt; array( 'id' =&gt; 'zip', ), 'options' =&gt; array( 'label' =&gt; 'Type', 'value_options' =&gt; array( '' =&gt; 'Select Type', 'default' =&gt; 'Default', 'invoice' =&gt; 'Invoice', 'both' =&gt; 'Both', ) ) )); } public function getClone($prefix){ $form = clone $this; foreach($form-&gt;getElements() as $element){ $name = $element-&gt;getName(); $element-&gt;setName("$prefix[$name]"); } return $form; } public function getInputFilter() { if (!$this-&gt;inputFilter) { $inputFilter = new InputFilter(); $factory = new InputFactory(); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'street_1', 'required' =&gt; true, 'filters' =&gt; array( array('name' =&gt; 'StripTags'), array('name' =&gt; 'StringTrim'), ), 'validators' =&gt; array( array( 'name' =&gt; 'NotEmpty', 'options' =&gt; array('message' =&gt; 'Street cannot be empty'), ), ), ))); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'street_2', 'required' =&gt; false, 'filters' =&gt; array( array('name' =&gt; 'StripTags'), array('name' =&gt; 'StringTrim'), ), 'validators' =&gt; array( ), ))); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'city_id', 'required' =&gt; true, 'filters' =&gt; array( array('name' =&gt; 'StripTags'), array('name' =&gt; 'StringTrim'), ), 'validators' =&gt; array( array( 'name' =&gt; 'NotEmpty', 'options' =&gt; array('message' =&gt; 'City cannot be empty'), ), ), ))); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'country_id', 'required' =&gt; true, 'filters' =&gt; array( array('name' =&gt; 'StripTags'), array('name' =&gt; 'StringTrim'), ), 'validators' =&gt; array( array( 'name' =&gt; 'NotEmpty', 'options' =&gt; array('message' =&gt; 'Country cannot be empty'), ), ), ))); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'zip', 'required' =&gt; true, 'filters' =&gt; array( array('name' =&gt; 'StripTags'), array('name' =&gt; 'StringTrim'), ), 'validators' =&gt; array( array( 'name' =&gt; 'NotEmpty', 'options' =&gt; array('message' =&gt; 'ZIP cannot be empty'), ), ), ))); $inputFilter-&gt;add($factory-&gt;createInput(array( 'name' =&gt; 'address_type', 'required' =&gt; true, 'filters' =&gt; array( array('name' =&gt; 'StripTags'), array('name' =&gt; 'StringTrim'), ), 'validators' =&gt; array( array( 'name' =&gt; 'NotEmpty', 'options' =&gt; array('message' =&gt; 'Address Type cannot be empty'), ), ), ))); $this-&gt;inputFilter = $inputFilter; } return $this-&gt;inputFilter; } } </code></pre> <p>and than in my action I did this </p> <pre><code>public function testAction() { $countries = $this-&gt;getServiceLocator()-&gt;get('Country')-&gt;getSelect(); $companyAddressForm = new CompanyAddressForm($countries); $clone = $companyAddressForm-&gt;getClone('address2'); if($this-&gt;request-&gt;isPost()){ $data = $this-&gt;request-&gt;getPost(); $companyAddressForm-&gt;setData($data); $clone-&gt;setData($data['address2']); $isFormOneValid = $companyAddressForm-&gt;isValid(); //$isFormTwoValid = $clone-&gt;isValid(); } $view = new ViewModel(); $view-&gt;companyAddressForm = $companyAddressForm; $view-&gt;clone = $clone; return $view; } </code></pre> <p>This is working as I expected it to work, forms are rendering and validating correctly, I want to know whether it is a correct way or a hack?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload