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.
public function render($content)
{
$innerHTML = "";
//Create a new DOM document
$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
//Parse the HTML. The @ is used to suppress any parsing errors
//that will be thrown if the $html string isn't valid XHTML.
@$dom->loadHTML($content);
//Get all inputs. You could also use any other tag name here,
//like 'img' or 'table', to extract other tags.
$eInputs = $dom->getElementsByTagName('input');
//Iterate over the extracted dds
foreach ($eInputs as $eInput) {
if ($eInput->getAttribute('type') == "text") {
$tmp_doc = new DOMDocument();
$tmp_doc->appendChild($tmp_doc->importNode($eInput,true));
$innerHTML .= $tmp_doc->saveHTML()." ";
} else if ($eInput->getAttribute('type') == "password") {
$tmp_doc = new DOMDocument();
$tmp_doc->appendChild($tmp_doc->importNode($eInput,true));
$innerHTML .= $tmp_doc->saveHTML()." ";
}
}
$inputs = explode(" ", $innerHTML);
foreach ($inputs as $input) {
if (!empty($input)) {
$input = str_replace(">", " />", $input);
$input = substr($input, 0, -1);
$replace = '<div class="content-border-bottom-center">
<div class="content-border-top-center">
<div class="content-border-corner-bottom-right">
<div class="content-border-corner-bottom-left">
<div class="content-border-corner-top-right">
<div class="content-border-corner-top-left">
'.$input.'
</div>
</div>
</div>
</div>
</div>
</div>';
$content = str_replace($input, $replace, $content);
}
}
return $content;
}