Created
June 29, 2012 20:31
-
-
Save bakura10/3020464 to your computer and use it in GitHub Desktop.
Form Row Helper use cases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The form row helper is a utility helper that allow to reduce the boilerplate code of a view file by automatically generating a label (if specified), a form element and errors that may arise during validation. | |
Simple example | |
Let's add an element : | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Color', | |
'name' => 'one_color' | |
)); | |
In the view, just write : | |
<?php echo $this->formRow($form->get('one_color')); ?> | |
It will produces the following markup : | |
<input type="color" name="one_color"> | |
You can add a label attribute in the form : | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Color', | |
'name' => 'one_color', | |
'attributes' => array( | |
'label' => 'This is a color' | |
) | |
)); | |
The following markup is generated : | |
<label>This is a color<input type="color" name="one_color"></label> | |
If you want the label to be prepended instead of being appended (which is the default), just give 'append' to the helper : | |
<?php echo $this->formRow($form->get('one_color'), 'append'); ?> | |
When used with multi checkboxes or radio, the row helper will wrap the options inside a fieldset tag, the label attribute being used as the legend. | |
Adding attribute to a label | |
For styling purposes, you may need to add specific attributes to the label created by the helper. To achieve this, just set a value to the rowLabelAttributes key : | |
$this->add(array( | |
'type' => 'Zend\Form\Element\Color', | |
'name' => 'one_color', | |
'attributes' => array( | |
'label' => 'This is a color', | |
'rowLabelAttributes' => array( | |
'class' => 'blue', | |
'data-key1' => 'value1' | |
) | |
) | |
)); | |
This will generate the following markup : | |
<label class="blue" data-key1="value1">This is a color<input type="color" name="one_color"></label> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment