【问题标题】:How to use existing decorators in Zend Framework?如何在 Zend Framework 中使用现有的装饰器?
【发布时间】:2010-11-29 13:33:11
【问题描述】:

以下代码:

$this->addElement('text', 'email', array(
    'label' => 'Your email address:',
));

$this->addElement('submit', 'submit', array(
    'label' => 'Sign Guestbook',
));

生成以下 HTML:

<form enctype="application/x-www-form-urlencoded" action="" method="post">
    <dl class="zend_form">
        <dt id="email-label">
            <label for="email" class="optional">Your email address:</label>
        </dt>
        <dd id="email-element">
            <input type="text" name="email" id="email" value="" />
        </dd>

        <dt id="submit-label">
            &#160;
        </dt>
        <dd id="submit-element">
            <input type="submit" name="submit" id="submit" value="Sign Guestbook" />
        </dd>
    </dl>
</form>

我知道,我可以编写自己的装饰器,但我想知道,如何使用现有的装饰器来创建以下 HTML:

<form enctype="application/x-www-form-urlencoded" action="" method="post">
    <div>
        <label for="email" class="optional">Your email address:</label>
        <input type="text" name="email" id="email" value="" class="my_class" />
    </div>

    <div>
        <input type="submit" name="submit" id="submit" value="Sign Guestbook" class="my_class" />
    </div>
</form>

没有&lt;dl/&gt;&lt;dt/&gt;&lt;dd/&gt;,添加了class属性。

比如我知道,怎么去掉周围的&lt;dl/&gt;标签:

$this->addDecorator('FormElements')
     ->addDecorator('Form');

是否可以在编写自定义装饰器的情况下进行其他更改?

【问题讨论】:

    标签: php html zend-framework zend-form zend-decorators


    【解决方案1】:

    这个(将此数组附加到您的参数数组,现在只包含一个标签选项)应该有助于您的电子邮件字段:

    'class' => 'my_class',
    'decorators' => array(
        'ViewHelper',
        'Errors',
        'Label',
        array('HtmlTag', array('tag' => 'div'))
    )
    

    同样没有Label - 用于提交。

    【讨论】:

    【解决方案2】:

    您可以使用 HtmlTag 装饰器并传递要使用的标签作为参数。

    这里列出了所有可用的装饰器:http://framework.zend.com/manual/en/zend.form.standardDecorators.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-20
      • 2011-06-27
      • 2012-10-23
      • 2012-10-08
      • 2010-11-02
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多