Form自定义HTML标记



需要一个关于如何创建自定义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>

假设表单元素命名为usernamepassword,下面的示例将给出所需的输出

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

最新更新