I am using Zend Framework in my project. I want to add a description/note to my forms, like
fields marked by * are mandatory
But i didn't found how to add a description to a form and how to use it with decorators.
Any help will be highly appreciated. Thanks.
There are two options:
Zend_Form_Element
to create a custom
elementI would go with the latter, since it is very common to add parts of raw html code to forms not only before elements or after, but among them also.
You should do something like this:
class My_Form_Element_Raw extends Zend_Form_Element
{
protected $raw_html;
public function setRawHtml($value)
{
$this->raw_html = $value;
return $this;
}
public function getRawHtml()
{
return $this->raw_html;
}
public function render()
{
// you can use decorators here yourself if you want, or wrap html in container tags
return $this->raw_html;
}
}
$form = new Zend_Form();
// add elements
$form->addElement(
new My_Form_Element_Raw(
'my_raw_element',
array('raw_html' => '<p class="highlight">fields marked by * are mandatory</p>')
)
);
echo $form->render();
When extending Zend_Form_Element
you dont need to overide setOption/s
, getOption/s
methods.
Zend internally uses set* and get* and protected properties to detect element options like in this case protected $raw_html;
and public function setRawHtml($value)
and public function getRawHtml()
Also naming your property $raw_html
will accept both options of 'raw_html' and 'rawHtml' respectively