Caution: The documentation you are viewing is
for an older version of Zend Framework.
You can find the documentation of the current version at:
Rendering Individual Decorators - Understanding and Using Zend Form 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:
If we wanted to pull and render just the SimpleInput decorator, we can do so using the getDecorator() method:
This is pretty easy, but it can be made even easier; let's do it in a single line:
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:
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:
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.
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:
If you use the above view script, you'll get approximately the following HTML (approximate, as the HTML below is formatted):
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.