I recently needed to add some custom HTML within a form. It proved to be easy but I did need to write some custom code to do it.
First, I had to create a HTML form element by subclassing Zend_Form_Element
class Misc_FormHtml extends Zend_Form_Element_Xhtml{ /** * Load default decorators * * @return Zend_Form_Element */ public function loadDefaultDecorators() { if ($this->loadDefaultDecoratorsIsDisabled()) { return $this; } $decorators = $this->getDecorators(); if (empty($decorators)) { $getId = create_function('$decorator', 'return $decorator->getElement()->getId() . "-element";'); $this ->addDecorator(new Decorators_Html()) ->addDecorator('HtmlTag', array('tag' => 'div', 'id' => array('callback' => $getId))) ; } return $this; } }
Zend loads the decorators from the inside out so in order to wrap my HTML in a div tag, I need to add a custom decorator (new Decorators_Html() ) first and then add the HtmlTag decorator second
I wrote a very simple decorator.
class Decorators_Html extends Zend_Form_Decorator_Abstract { public function render($content) { //return $content; $element = $this->getElement(); $value = $element->getValue(); return $content.$value ; } }
Then I set it up to be used:
... form code $html = new Misc_FormHtml(); $html->setValue('Here is some text that we are really hoping will appear on the page without being messed up by Zend
') ->setAttrib('id','top_form_text'); $category_sub_form->addElement($html);