Rendering Individual Decorators

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:

  1. $element = new Zend_Form_Element('foo', array(
  2.     'label'      => 'Foo',
  3.     'belongsTo'  => 'bar',
  4.     'value'      => 'test',
  5.     'prefixPath' => array('decorator' => array(
  6.         'My_Decorator' => 'path/to/decorators/',
  7.     )),
  8.     'decorators' => array(
  9.         '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:

  1. $decorator = $element->getDecorator('SimpleInput');
  2. echo $decorator->render('');

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

  1. 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:

  1. 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:

  1. class My_Form_UserDemographics extends Zend_Form
  2. {
  3.     public function init()
  4.     {
  5.         // Add a path for my own decorators
  6.         $this->addElementPrefixPaths(array(
  7.             'decorator' => array('My_Decorator' => 'My/Decorator'),
  8.         ));
  9.  
  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:

  1. <?php
  2. $form = $this->form;
  3. // Remove <dt> from label generation
  4. foreach ($form->getElements() as $element) {
  5.     $element->getDecorator('label')->setOption('tag', null);
  6. }
  7. ?>
  8. <form method="<?php echo $form->getMethod() ?>" action="<?php echo
  9.     $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">
  34.         <?php echo $form->passwordConfirmation->renderLabel()
  35.               . $form->passwordConfirmation->renderViewHelper() ?>
  36.     </div>
  37.     <?php echo $this->formSubmit('submit', 'Save') ?>
  38. </form>

If you use the above view script, you'll get approximately the following HTML (approximate, as the HTML below is formatted):

  1. <form method="post" action="">
  2.     <div class="element">
  3.         <label for="title" tag="" class="optional">Title:</label>
  4.         <input type="text" name="title" id="title" value=""/>
  5.  
  6.         <label for="firstName" tag="" class="optional">First name:</label>
  7.         <input type="text" name="firstName" id="firstName" value=""/>
  8.  
  9.         <label for="lastName" tag="" class="optional">Last name:</label>
  10.         <input type="text" name="lastName" id="lastName" value=""/>
  11.     </div>
  12.  
  13.     <div class="element">
  14.         <label for="dateOfBirth" tag="" class="optional">Date of Birth
  15.             (DD/MM/YYYY):</label>
  16.         <input type="text" name="dateOfBirth[day]" id="dateOfBirth-day"
  17.             value="" size="2" maxlength="2"/>
  18.         /
  19.         <input type="text" name="dateOfBirth[month]" id="dateOfBirth-month"
  20.             value="" size="2" maxlength="2"/>
  21.         /
  22.         <input type="text" name="dateOfBirth[year]" id="dateOfBirth-year"
  23.             value="" size="4" maxlength="4"/>
  24.     </div>
  25.  
  26.     <div class="element">
  27.         <label for="password" tag="" class="optional">Password:</label>
  28.         <input type="password" name="password" id="password" value=""/>
  29.     </div>
  30.  
  31.     <div class="element">
  32.         <label for="passwordConfirmation" tag="" class="" id="submit"
  33.             value="Save"/>
  34. </form>

It may not be truly pretty, but with some CSS, it could be made to look exactly how you might want to see it. The main point, however, is that this form was generated using almost entirely custom markup, while still leveraging decorators for the most common markup (and to ensure things like escaping with htmlentities and value injection occur).

By this point in the tutorial, you should be getting fairly comfortable with the markup possibilities using Zend_Form's decorators. In the next section, we'll revisit the date element from above, and demonstrate how to create a custom element and decorator for composite elements.

blog comments powered by Disqus