Skip to content

forms customization

Harmen Janssen edited this page Aug 22, 2016 · 4 revisions

Customizing forms

Setting custom attributes on an element

Most attributes can be included in $options:

$form->addElement('text', 'something', array(
	'autocomplete' => 'off'
));

Sometimes an option-name is reserved by Zend for another feature however, so just remember that you can always add attributes like this:

$myField = $form->createElement('text', 'something');
$myField->setAttrib('autocomplete', 'off');
$form->addElement($myField);

Printing the label behind the input element

This is especially valuable when rendering checkboxes:

$checkbox = $form->createElement('checkbox', 'legal', array(
	'label' => 'I\'ve read the terms and conditions'
));
$checkbox->getDecorator('Label')->setOption('placement', 'append');
$form->addElement($checkbox);

Add HTML to the label element

For instance: I agree to the terms and conditions. Simply add 'escape' as an option:

$form->addElement('checkbox', 'terms', array(
'label' => 'I agree to the <a href="#">terms and conditions</a>',
'escape' => false
));

Adding a descriptive line to a field

$form->addElement('file', 'avatar', array(
	'label' => 'Upload your avatar',
	'description' => 'max 20MB, allowed extentions: JPG, GIF, PNG'
));

Adding custom HTML to the form

$form->addElement('html', 'h2', array(
'value' => 'feedback & suggestions',
'class' => 'foobar'
));
// Renders: <h2 class="foobar">feedback &amp; suggestions</h2>

Change the "required label suffix"

class App_Form_EducationBooking extends Garp_Form {
    protected $_defaultRequiredLabelSuffix = '⭐️';

    public function init() {
        parent::init();

        // ...
    }
}

Back to forms

Clone this wiki locally