views:

295

answers:

2

How do I achieve the following with form decorators for form elements:

<dt>
  <ul>
    <li>The errors</li>
    <li>The errors</li>
  </ul>
  <label>The label</label>
</dt>
<dd>
  <input type="text" value="The input field">
</dd>

In other words, in stead of Errors appended after the input field, I want them prepended before the Label. I do however want to keep the <dt> and <dd> tags as illustrated above.

A: 

in your form class try

$this->setElementDecorators(array(
    'Errors',
    'ViewHelper',
    'Label',
));
solomongaby
This doesn't cut it I'm afraid. This results in `<ul></ul><input/><label></label>`, and without `<dt>` and `<dd>`'s
fireeyedboy
+1  A: 

Alright, I found out how to do it. Gradually the decorators are starting to make sense to me:

$decorators = array(
 'Label',
 array( 'Errors', array( 'placement' => 'prepend' ) ),
 array( array( 'dt' => 'HtmlTag' ), array( 'tag' => 'dt' ) ),
 array( array( 'ddOpen' => 'HtmlTag' ), array( 'tag' => 'dd', 'openOnly' => true, 'placement' => 'append' ) ),
 array( 'ViewHelper' ),
 array( array( 'ddClose' => 'HtmlTag' ), array( 'tag' => 'dd', 'closeOnly' => true, 'placement' => 'append' ) )
);

What this does is the following:

  1. First render the Label
  2. Then prepend (default = append) the Errors
  3. Wrap (default) all previous content in a HtmlTag (dt)
  4. Next, append (default = wrap) a opening HtmlTag (dd)
  5. Then append (default) the ViewHelper
  6. Next, append (default = wrap) a closing HtmlTag (dd)

Then set the decorators:

// be sure to only set them, after you have added the relevant elements to the form
$this->setElementDecorators( $decorators );

PS:
Be aware though that my particular example produces invaliid html. ;-) I only found out later that <ul> elements are not allowed in <dt> elements with DOCTYPE HTML 4.01 strict

fireeyedboy
Make sure you accept your answer, so it shows up as solved :)
devians
I will, but you can't accept your own answer within 24h.
fireeyedboy