Friday, November 11, 2011

[ZF] Containing form elements in DIVs (or other HTML tags) with classes

Long time, no see.

So, let's say you're building a dynamic form and would like each dt (label) and dd (input field) pair to be grouped in a div,with one or more specific classes added to this div.

It's quite easy, actually!
// Wrap element in div with classes
$element->addDecorator(
array('openDiv' =>'HtmlTag'),
array(
'tag' => 'div',
'openOnly' => true,
'class' => $attrib_class
)
);
$element->addDecorator(
array('closeDiv' =>'HtmlTag'),
array('tag' => 'div', 'closeOnly' => true)
);

Considering your custom class name(s) are in the $attrib_class variable, this will output something similar to this:
<div class="class-1 class-2">
<dt id="item-id-label">
<label for="item-id">label</label>
</dt>
<dd id="item-id-element">
<input type="text" name="item-id" id="item-id" value="">
</dd>
</div>

And there you have it, customizable form elements!

No comments:

Post a Comment