【问题标题】:Zend\Form Custom HTML markupZend\Form 自定义 HTML 标记
【发布时间】:2013-02-28 08:33:59
【问题描述】:

需要有关如何创建自定义 HTML 标记的教程(如 Zend 1 上的表单装饰器)。 我想生成类似的东西:

<ul>
<li>
    <label class="required"><span>*</span> Username</label>
    <input id="username" name="username" type="text">
</li>
<li>
    <label class="required"><span>*</span> Password</label>
    <input id="password" name="password" type="password">
</li>
</ul>

【问题讨论】:

  • 你只需要实现你自己的视图助手来处理这种标记。就这么简单:)

标签: zend-framework2 zend-form2


【解决方案1】:

假设表单元素被命名为 username 和 password,下面是一个示例,可以提供所需的输出

<?php
// attributes to apply to label(s)
$labelAttr = array('class' => 'required');
// extra label content (assumes Username and Password are already present in the given elements)
$labelSpan = '<span>*</span> ';
// set the label attributes
$form->get('username')->setLabelAttributes($labelAttr);
$form->get('password')->setLabelAttributes($labelAttr);
?>

<ul>
<li>
    // use the formLabel helper, hand it the extra label content, and tell it to place the existing label after it
    <?php echo $this->formLabel($form->get('username'), $labelSpan, 'append');
    <?php echo $this->formInput($form->get('username');
</li>
<li>
    <?php echo $this->formLabel($form->get('password'), $labelSpan, 'append');
    <?php echo $this->formInput($form->get('password');
</li>
</ul>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多