I've grown to love Zend_Form and decorators. To bend them to my will, I've
made a few extension classes that house commonly used items (such as
decorator setups), which has drastically reduced form creation time.

I couldn't find this specifically mentioned in the manual (although I think
it was in an older version), but here's a breakdown of how to format the
setDecorators() input array:

// array of decorator names - this happens to be the typical default set of
decorators
$decorators = array(
'ViewHelper',
        'Errors',
        'Description',
        'HtmlTag',
        'Label'
);

// Another good thing to know is that the decorators are rendered as a chain
in order (does a foreach across the decorator array)

// array of arrays where first entry is the decorator name, second is the
options (array keys are optional, but if used, the order doesn't matter)
// You can mix the methods as well (for instance I'll start with just
'ViewHelper'
$decorators = array(
        'ViewHelper',
array(
'decorator' => 'Label',
 'options' => array(
'escape' => false,
'separator' => '<br />'
 )
),
array(
 'Errors',
array(
'class' => 'error'
 )
),
        array(
'HtmlTag',
 array(
'tag' => 'p'
)
 )
);

Any option you put that isn't a standard option gets added to the rendered
element as an html attribute. For instance, the 'class'=>'error' in the
Errors decorator gets added to the html wrapper to the errors list (default
is a ul tag, so <ul class="error">)

You can repeat the same decorator in the chain, but you have to give it a
different key, such as:
$decorators = array(
     ...
    'label1' => 'Label',
    'label2' => 'Label',
    ...
);
Why you would want two labels like that, I don't know, but where it comes in
handy is if you need to use multiple html tags, which gets a bit trickier:
$decorators = array(
     ...
    array(
        'decorator' => array( 'tag1' => 'HtmlTag' ),
        'options' => array( 'tag' => 'p' )
    ),
    array(
        'decorator' => array( 'tag2' => 'HtmlTag' ),
        'options' => array( 'tag' => 'div' )
    ),
    ...
);
(This option isn't reflected in the API as it says addDecorator only accepts
a string or Zend_Form_Decorator_Interface for $decorator, but it also
accepts an array as is clearly reflected if you look in the code.)

The real power is combining the above with your own custom form decorators
to make it do what you want.

Another good thing to keep in mind is that the decorators typically call on
view helpers to render the actual html. So, sometimes you just need to
extend a view helper, instead of the form decorator.

Hope this helps,
James Ganong


On Thu, Sep 29, 2011 at 2:24 AM, David Muir <[email protected]> wrote:

> The way I handle forms is like this:
>
> <table>
>  <tr>
>    <td><?=$firstname->renderLabel()?></td>
>    <td><?=$firstname->renderViewHelper()?><br />
>      <?=$firstname->renderErrors()?></td>
>  </tr>
>  …etc…
>
> </table>
>
> Except I wouldn't be using tables, but fieldsets and divs instead.
>
> Cheers,
> David
>
>
> On 29/09/11 07:10, Christof Coetzee wrote:
> > ...here is a simple example of what I'm struggling with, would appreciate
> if
> > someone can show me how to do this with decorators the easy way.
> >
> > thanks in advance.
> >
> > <table>
> >   <tr>
> >     <td>firstnameLabel</td>
> >     <td>firstnameTextField<br />
> >       firstnameErrorMessage</td>
> >   </tr>
> >   <tr>
> >     <td>emailLookupButton</td>
> >     <td>emailLookupLabel
> >       <br />
> >       emailLookupTextField
> >       <br />
> >     emailLookupErrorMessage    </td>
> >   </tr>
> >   <tr>
> >     <td>ageLabel</td>
> >     <td>
> >     <table style="float:right">
> >       <tr>
> >         <td>ageMonthSelectField</td>
> >         <td>ageYearSelectField</td>
> >       </tr>
> >     </table>
> >     </td>
> >   </tr>
> > </table>
> >
> > --
> > View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/Over-engineered-Zend-Form-Decorators-help-tp3850650p3853335.html
> > Sent from the Zend Framework mailing list archive at Nabble.com.
> >
>
>
> --
> List: [email protected]
> Info: http://framework.zend.com/archives
> Unsubscribe: [email protected]
>
>
>

Reply via email to