【问题标题】:Zend Form decorators troubleZend Form 装饰器的麻烦
【发布时间】:2011-01-09 02:10:15
【问题描述】:

如何使用表单元素的表单装饰器实现以下目标:

<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>

换句话说,我希望它们不是附加在输入字段之后的错误,而是附加在标签之前。我确实但是想保留&lt;dt&gt;&lt;dd&gt; 标签,如上图所示。

【问题讨论】:

    标签: zend-framework zend-form decorator


    【解决方案1】:

    好的,我知道怎么做。渐渐地,装饰器开始对我有意义:

    $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' ) )
    );
    

    它的作用如下:

    1. 首先渲染标签
    2. 然后添加(默认 = 附加)错误
    3. 在 HtmlTag (dt) 中包装(默认)所有以前的内容
    4. 接下来,附加(默认 = 换行)一个打开的 HtmlTag (dd)
    5. 然后附加(默认)ViewHelper
    6. 接下来,附加(默认 = wrap)一个结束 HtmlTag (dd)

    然后设置装饰器:

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

    PS:
    请注意,尽管我的特定示例会产生无效的 html。 ;-) 我后来才发现 &lt;dt&gt; 元素中不允许使用 &lt;ul&gt; 元素和 DOCTYPE HTML 4.01 strict

    【讨论】:

    • 确保你接受你的答案,所以它显示为已解决:)
    【解决方案2】:

    在你的表单类中,试试这个:

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

    【讨论】:

    • 恐怕这不会削减它。这导致&lt;ul&gt;&lt;/ul&gt;&lt;input/&gt;&lt;label&gt;&lt;/label&gt;,没有&lt;dt&gt;&lt;dd&gt;'s
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    相关资源
    最近更新 更多