Understanding and Using Zend Form Decorators. Rendering Individual Decorators

Translations of this material:

into Russian: 6.4 Понимание и использование Zend Form Decorators. Визуализация отдельных декораторов. 0% translated in draft.
Submitted for translation by antdmi 20.02.2011


In the previous section, we looked at how you can combine decorators to create complex output. We noted that while you have a ton of flexibility with this approach, it also adds some complexity and overhead. In this section, we will examine how to render decorators individually in order to create custom markup for forms and/or individual elements.

Once you have registered your decorators, you can later retrieve them by name from the element. Let's review the previous example:

  01. $element = new Zend_Form_Element('foo', array(

  02.     'label'     => 'Foo',

  03.     'belongsTo' => 'bar',

  04.     'value'     => 'test',

  05.     'prefixPath' => array('decorator' => array(

  06.         'My_Decorator' => 'path/to/decorators/',

  07.     )),

  08.     'decorators' => array(

  09.         'SimpleInput'

  10.         array('SimpleLabel', array('placement' => 'append')),

  11.     ),

  12. ));

If we wanted to pull and render just the SimpleInput decorator, we can do so using the getDecorator() method:

  01. $decorator = $element->getDecorator('SimpleInput');

  02. echo $decorator->render('');

This is pretty easy, but it can be made even easier; let's do it in a single line:

  01. echo $element->getDecorator('SimpleInput')->render('');

Not too bad, but still a little complex. To make this easier, a shorthand notation was introduced into Zend_Form in 1.7: you can render any registered decorator by calling a method of the format renderDecoratorName(). This will effectively perform what you see above, but makes the $content argument optional and simplifies the usage:

  01. echo $element->renderSimpleInput();

This is a neat trick, but how and why would you use it?

Many developers and designers have very precise markup needs for their forms. They would rather have full control over the output than rely on a more automated solution which may or may not conform to their design. In other cases, the form layout may require a lot of specialized markup -- grouping arbitrary elements, making some invisible unless a particular link is selected, etc.

Let's utilize the ability to render individual decorators to create some specialized markup.

First, let's define a form. Our form will capture a user's demographic details. The markup will be highly customized, and in some cases use view helpers directly instead of form elements in order to achieve its goals. Here is the basic form definition:

  01. class My_Form_UserDemographics extends Zend_Form

  02. {

  03.     public function init()

  04.     {

  05.         // Add a path for my own decorators

  06.         $this->addElementPrefixPaths(array(

  07.             'decorator' => array('My_Decorator' => 'My/Decorator'),

  08.         ));


  10.         $this->addElement('text', 'firstName', array(

  11.             'label' => 'First name: ',

  12.         ));

  13.         $this->addElement('text', 'lastName', array(

  14.             'label' => 'Last name: ',

  15.         ));

  16.         $this->addElement('text', 'title', array(

  17.             'label' => 'Title: ',

  18.         ));

  19.         $this->addElement('text', 'dateOfBirth', array(

  20.             'label' => 'Date of Birth (DD/MM/YYYY): ',

  21.         ));

  22.         $this->addElement('text', 'email', array(

  23.             'label' => 'Your email address: ',

  24.         ));

  25.         $this->addElement('password', 'password', array(

  26.             'label' => 'Password: ',

  27.         ));

  28.         $this->addElement('password', 'passwordConfirmation', array(

  29.             'label' => 'Confirm Password: ',

  30.         ));

  31.     }

  32. }

    Note: We're not defining any validators or filters at this time, as they are not relevant to the discussion of decoration. In a real-world scenario, you should define them.

With that out of the way, let's consider how we might want to display this form. One common idiom with first/last names is to display them on a single line; when a title is provided, that is often on the same line as well. Dates, when not using a JavaScript date chooser, will often be separated into three fields displayed side by side.

Let's use the ability to render an element's decorators one by one to accomplish this. First, let's note that no explicit decorators were defined for the given elements. As a refresher, the default decorators for (most) elements are:

    • ViewHelper: utilize a view helper to render a form input

    • Errors: utilize the FormErrors view helper to render validation errors

    • Description: utilize the FormNote view helper to render the element description (if any)

    • HtmlTag: wrap the above three items in a <dd> tag

    • Label: render the element label using the FormLabel view helper (and wrap it in a <dt> tag)

Also, as a refresher, you can access any element of a form as if it were a class property; simply reference the element by the name you assigned it.

Our view script might then look like this:

  01. <?php

  02. $form = $this->form;

  03. // Remove <dt> from label generation

  04. foreach ($form->getElements() as $element) {

  05.     $element->getDecorator('label')->setOption('tag', null);

  06. }

  07. ?>

  08. <form method="<?php echo $form->getMethod() ?>" action="<?php echo

  09.     $form->getAction()?>">

  10.     <div class="element">

  11.         <?php echo $form->title->renderLabel()

  12.              . $form->title->renderViewHelper() ?>

  13.         <?php echo $form->firstName->renderLabel()

  14.              . $form->firstName->renderViewHelper() ?>

  15.         <?php echo $form->lastName->renderLabel()

  16.              . $form->lastName->renderViewHelper() ?>

  17.     </div>

  18.     <div class="element">

  19.         <?php echo $form->dateOfBirth->renderLabel() ?>

  20.         <?php echo $this->formText('dateOfBirth[day]', '', array(

  21.             'size' => 2, 'maxlength' => 2)) ?>

  22.         /

  23.         <?php echo $this->formText('dateOfBirth[month]', '', array(

  24.             'size' => 2, 'maxlength' => 2)) ?>

  25.         /

  26.         <?php echo $this->formText('dateOfBirth[year]', '', array(

  27.             'size' => 4, 'maxlength' => 4)) ?>

  28.     </div>

  29.     <div class="element">

  30.         <?php echo $form->password->renderLabel()

  31.              . $form->password->renderViewHelper() ?>

  32.     </div>

  33.     <div class="element">

Pages: ← previous Ctrl next
1 2