I’m looking for a way to wrap zend_form form elements inside div’s.
I can get the desired result by using the code below inside the form class.
$element->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array(array('top-left' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-corner-top-left')),
array(array('top-right' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-corner-top-right')),
array(array('top-center' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-top-center')),
array(array('bottom-left' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-corner-bottom-left')),
array(array('bottom-right' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-corner-bottom-right')),
array(array('bottom-center' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-bottom-center')),
array(array('left' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-left')),
array(array('right' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-right')),
array(array('dd' => 'HtmlTag'), array('tag' => 'dd', 'id' => $element->getLabel().'-element')),
array('Label', array('tag' => 'dt')),
));
I would like to know if I could use custom decorators to achieve the desired result.
The code above is very easy to implement but has to be done for every element.
So then I thought, could I use a custom decorator to achieve the same result?
So far I’ve not been able to, which is why I’m asking this question here.
::Edit::
I forgot to mention what I’ve been trying to do so far.
I have been trying to break down the form inside my custom decorator.
But I’ve had no luck so far.
class Form_Decorator_Borders extends Zend_Form_Decorator_Abstract
{
public function render($content)
{
$element = $this->getElement(); // get form
$elements = $element->getElements(); // get form elements
$placement = $this->getPlacement();
$name = htmlentities($element->getFullyQualifiedName());
$id = htmlentities($element->getId());
foreach ($elements as $k => $v) {
if (is_object($v) && get_class($v) == "Zend_Form_Element_Text") {
$elements[$k]->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array(array('top-left' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-corner-top-left')),
array(array('top-right' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-corner-top-right')),
array(array('top-center' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-top-center')),
array(array('bottom-left' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-corner-bottom-left')),
array(array('bottom-right' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-corner-bottom-right')),
array(array('bottom-center' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-bottom-center')),
array(array('left' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-left')),
array(array('right' => 'HtmlTag'), array('tag' => 'div', 'class' => 'content-border-right')),
array(array('dd' => 'HtmlTag'), array('tag' => 'dd', 'id' => $elements[$k]->getLabel().'-element')),
array('Label', array('tag' => 'dt')),
));
}
}
$element->setElements($elements);
$this->setElement($element);
$this->setElement($element);
return $this->getElement()->getView()->render($name);
}
}
I’ve come to use a different approach.
This is what I’m currently using and is working fine so far.