Documentation

Creating and Rendering Composite Elements - Understanding and Using Zend Form Decorators

Creating and Rendering Composite Elements

In the last section, we had an example showing a "date of birth element":

  1. <div class="element">
  2.     <?php echo $form->dateOfBirth->renderLabel() ?>
  3.     <?php echo $this->formText('dateOfBirth[day]', '', array(
  4.         'size' => 2, 'maxlength' => 2)) ?>
  5.     /
  6.     <?php echo $this->formText('dateOfBirth[month]', '', array(
  7.         'size' => 2, 'maxlength' => 2)) ?>
  8.     /
  9.     <?php echo $this->formText('dateOfBirth[year]', '', array(
  10.         'size' => 4, 'maxlength' => 4)) ?>
  11. </div>

How might you represent this element as a Zend_Form_Element? How might you write a decorator to render it?

The Element

The questions about how the element would work include:

  • How would you set and retrieve the value?

  • How would you validate the value?

  • Regardless, how would you then allow for discrete form inputs for the three segments (day, month, year)?

The first two questions center around the form element itself: how would setValue() and getValue() work? There's actually another question implied by the question about the decorator: how would you retrieve the discrete date segments from the element and/or set them?

The solution is to override the setValue() method of your element to provide some custom logic. In this particular case, our element should have three discrete behaviors:

  • If an integer timestamp is provided, it should be used to determine and store the day, month, and year.

  • If a textual string is provided, it should be cast to a timestamp, and then that value used to determine and store the day, month, and year.

  • If an array containing keys for date, month, and year is provided, those values should be stored.

Internally, the day, month, and year will be stored discretely. When the value of the element is retrieved, it will be done so in a normalized string format. We'll override getValue() as well to assemble the discrete date segments into a final string.

Here's what the class would look like:

  1. class My_Form_Element_Date extends Zend_Form_Element_Xhtml
  2. {
  3.     protected $_dateFormat = '%year%-%month%-%day%';
  4.     protected $_day;
  5.     protected $_month;
  6.     protected $_year;
  7.  
  8.     public function setDay($value)
  9.     {
  10.         $this->_day = (int) $value;
  11.         return $this;
  12.     }
  13.  
  14.     public function getDay()
  15.     {
  16.         return $this->_day;
  17.     }
  18.  
  19.     public function setMonth($value)
  20.     {
  21.         $this->_month = (int) $value;
  22.         return $this;
  23.     }
  24.  
  25.     public function getMonth()
  26.     {
  27.         return $this->_month;
  28.     }
  29.  
  30.     public function setYear($value)
  31.     {
  32.         $this->_year = (int) $value;
  33.         return $this;
  34.     }
  35.  
  36.     public function getYear()
  37.     {
  38.         return $this->_year;
  39.     }
  40.  
  41.     public function setValue($value)
  42.     {
  43.         if (is_int($value)) {
  44.             $this->setDay(date('d', $value))
  45.                  ->setMonth(date('m', $value))
  46.                  ->setYear(date('Y', $value));
  47.         } elseif (is_string($value)) {
  48.             $date = strtotime($value);
  49.             $this->setDay(date('d', $date))
  50.                  ->setMonth(date('m', $date))
  51.                  ->setYear(date('Y', $date));
  52.         } elseif (is_array($value)
  53.                   && (isset($value['day'])
  54.                       && isset($value['month'])
  55.                       && isset($value['year'])
  56.                   )
  57.         ) {
  58.             $this->setDay($value['day'])
  59.                  ->setMonth($value['month'])
  60.                  ->setYear($value['year']);
  61.         } else {
  62.             throw new Exception('Invalid date value provided');
  63.         }
  64.  
  65.         return $this;
  66.     }
  67.  
  68.     public function getValue()
  69.     {
  70.         return str_replace(
  71.             array('%year%', '%month%', '%day%'),
  72.             array($this->getYear(), $this->getMonth(), $this->getDay()),
  73.             $this->_dateFormat
  74.         );
  75.     }
  76. }

This class gives some nice flexibility -- we can set default values from our database, and be certain that the value will be stored and represented correctly. Additionally, we can allow for the value to be set from an array passed via form input. Finally, we have discrete accessors for each date segment, which we can now use in a decorator to create a composite element.

The Decorator

Revisiting the example from the last section, let's assume that we want users to input each of the year, month, and day separately. PHP fortunately allows us to use array notation when creating elements, so it's still possible to capture these three entities into a single value -- and we've now created a Zend_Form element that can handle such an array value.

The decorator is relatively simple: it will grab the day, month, and year from the element, and pass each to a discrete view helper to render individual form inputs; these will then be aggregated to form the final markup.

  1. class My_Form_Decorator_Date extends Zend_Form_Decorator_Abstract
  2. {
  3.     public function render($content)
  4.     {
  5.         $element = $this->getElement();
  6.         if (!$element instanceof My_Form_Element_Date) {
  7.             // only want to render Date elements
  8.             return $content;
  9.         }
  10.  
  11.         $view = $element->getView();
  12.         if (!$view instanceof Zend_View_Interface) {
  13.             // using view helpers, so do nothing if no view present
  14.             return $content;
  15.         }
  16.  
  17.         $day   = $element->getDay();
  18.         $month = $element->getMonth();
  19.         $year  = $element->getYear();
  20.         $name  = $element->getFullyQualifiedName();
  21.  
  22.         $params = array(
  23.             'size'      => 2,
  24.             'maxlength' => 2,
  25.         );
  26.         $yearParams = array(
  27.             'size'      => 4,
  28.             'maxlength' => 4,
  29.         );
  30.  
  31.         $markup = $view->formText($name . '[day]', $day, $params)
  32.                 . ' / ' . $view->formText($name . '[month]', $month, $params)
  33.                 . ' / ' . $view->formText($name . '[year]', $year, $yearParams);
  34.  
  35.         switch ($this->getPlacement()) {
  36.             case self::PREPEND:
  37.                 return $markup . $this->getSeparator() . $content;
  38.             case self::APPEND:
  39.             default:
  40.                 return $content . $this->getSeparator() . $markup;
  41.         }
  42.     }
  43. }

We now have to do a minor tweak to our form element, and tell it that we want to use the above decorator as a default. That takes two steps. First, we need to inform the element of the decorator path. We can do that in the constructor:

  1. class My_Form_Element_Date extends Zend_Form_Element_Xhtml
  2. {
  3.     // ...
  4.  
  5.     public function __construct($spec, $options = null)
  6.     {
  7.         $this->addPrefixPath(
  8.             'My_Form_Decorator',
  9.             'My/Form/Decorator',
  10.             'decorator'
  11.         );
  12.         parent::__construct($spec, $options);
  13.     }
  14.  
  15.     // ...
  16. }

Note that this is being done in the constructor and not in init(). This is for two reasons. First, it allows extending the element later to add logic in init without needing to worry about calling parent::init(). Second, it allows passing additional plugin paths via configuration or within an init method that will then allow overriding the default Date decorator with my own replacement.

Next, we need to override the loadDefaultDecorators() method to use our new Date decorator:

  1. class My_Form_Element_Date extends Zend_Form_Element_Xhtml
  2. {
  3.     // ...
  4.  
  5.     public function loadDefaultDecorators()
  6.     {
  7.         if ($this->loadDefaultDecoratorsIsDisabled()) {
  8.             return;
  9.         }
  10.  
  11.         $decorators = $this->getDecorators();
  12.         if (empty($decorators)) {
  13.             $this->addDecorator('Date')
  14.                  ->addDecorator('Errors')
  15.                  ->addDecorator('Description', array(
  16.                      'tag'   => 'p',
  17.                      'class' => 'description'
  18.                  ))
  19.                  ->addDecorator('HtmlTag', array(
  20.                      'tag' => 'dd',
  21.                      'id'  => $this->getName() . '-element'
  22.                  ))
  23.                  ->addDecorator('Label', array('tag' => 'dt'));
  24.         }
  25.     }
  26.  
  27.     // ...
  28. }

What does the final output look like? Let's consider the following element:

  1. $d = new My_Form_Element_Date('dateOfBirth');
  2. $d->setLabel('Date of Birth: ')
  3.   ->setView(new Zend_View());
  4.  
  5. // These are equivalent:
  6. $d->setValue('20 April 2009');
  7. $d->setValue(array('year' => '2009', 'month' => '04', 'day' => '20'));

If you then echo this element, you get the following markup (with some slight whitespace modifications for readability):

  1. <dt id="dateOfBirth-label"><label for="dateOfBirth" class="optional">
  2.     Date of Birth:
  3. </label></dt>
  4. <dd id="dateOfBirth-element">
  5.     <input type="text" name="dateOfBirth[day]" id="dateOfBirth-day"
  6.         value="20" size="2" maxlength="2"> /
  7.     <input type="text" name="dateOfBirth[month]" id="dateOfBirth-month"
  8.         value="4" size="2" maxlength="2"> /
  9.     <input type="text" name="dateOfBirth[year]" id="dateOfBirth-year"
  10.         value="2009" size="4" maxlength="4">
  11. </dd>

Conclusion

We now have an element that can render multiple related form input fields, and then handle the aggregated fields as a single entity -- the dateOfBirth element will be passed as an array to the element, and the element will then, as we noted earlier, create the appropriate date segments and return a value we can use for most backends.

Additionally, we can use different decorators with the element. If we wanted to use a » Dojo DateTextBox dijit decorator -- which accepts and returns string values -- we can, with no modifications to the element itself.

In the end, you get a uniform element API you can use to describe an element representing a composite value.

Copyright

© 2006-2021 by Zend by Perforce. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts