I need to insert html code like this in my zend form:
<div class="myClass1" id="myId1" style="display: none;"><img src="images/myImage.jpg" /></div>
What will be the code in zend framework for above html code. I tried with Create_Element but it always create a button inside a div tag. Please give an example code. Thanks
This is quasi-finished in ZF 1.10. Seems someone made a Zend_View_Helper_FormNote class so you can insert arbitrary (cept for the decorator) HTML.
To use it you must extend Zend_Form_Element_Xhtml.
Then using the form plugin loader, add your form/element classes to the form object. I put it in my library folder (MyNs/Form/Element/Note.php).
Now you can call it just like any element.
As I mentioned before this still wraps your code in the decorator, but its a solution pre-built into ZF.
I created a custom element called "html". I added Html.php to /Form/Element:
Second I had to add a view helper FormHtml.php (I put it in application/views/helpers):
You can then add html to your form as follows:
There might be some more simplifications possible.
With above method the value of
FormNote
still disappears on submitting the form with validation errors!Edit: Can be deleted, with overwriting
isValid()
the problem is gone.I usually use description with array(escape => false) option to render HTML in form.
If you're creating a form element, I think you have to override the isValid() method, as in the code below, or your "value" will disappear upon a validation error:
Have you considered a form ViewScript? See this discussion.