开发者

Zend Form decorators trouble

开发者 https://www.devze.com 2022-12-17 06:13 出处:网络
How do I achieve the following with form decorators for form elements: <dt> <ul> <li>The errors</li>

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.


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


In your form class, try this:

$this->setElementDecorators(array(
    'Errors',
    'ViewHelper',
    'Label',
));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号