Kevin
Kevin

Reputation: 101

Zend\Form Custom HTML markup

need a tutorial about how to create custom HTML markup (like form decorators on Zend 1). I want to generate something like:

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

Upvotes: 0

Views: 351

Answers (1)

Crisp
Crisp

Reputation: 11447

Assuming the form elements are named username and password, here's an example that would give the desired output

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

Upvotes: 1

Related Questions