As a quick follow up to my above instructions on how to get a "multi form
element" to have custom HTML markup, it can be quite fast, depending on what
you want to do.
Problem:
You want to mark up each individual element in an class such as
Zend_Form_Element_MultiCheckbox
Example:
<ul>
<li><label>Foo</label></li>
[...]
</ul>
Quick Solution:
Custom View Helper.
1. Find out what view helper your multi element is using. Do this by
opening the the source file for the given element or viewing the API.
If you're looking for Zend_Form_Element_MultiCheckbox open
"/Zend/Form/Element/MultiCheckbox.php" and find the "$helper" variable and
what it's being set to. MultiCheckbox uses "formMultiCheckbox" view helper
or "Zend_View_Helper_FormMulticheckbox"
2. Open up that given helper. MultiCheckbox helper is
"/Zend/View/Helper/FormMulticheckbox.php".
3. Look for that helpers self named function. MultiCheckbox is called "
public function formMultiCheckbox"
4. If this function just called it's parent function, find out what view
helper that is, and open up that class. MultiCheckbox simply calls
"Zend_View_Helper_FormRadio".
5. Open up "Zend_View_Helper_FormRadio" in
"/Zend/View/Helper/FormRadio.php".
6. Verify that the self named function is rendering some HTML.
"Zend_View_Helper_FormRadio" does.
7. Copy "/Zend/View/Helper/FormRadio.php" to your
"/application/view/helper/" folder and rename it "MultiCheckboxList.php" so
you will end up with "/application/view/helper/MultiCheckboxList.php"
8. Open up and edit "MultiCheckboxList.php" in the following way:
8a. Change the class name to
"class Zend_View_Helper_FormMultiCheckboxList extends
Zend_View_Helper_FormElement"
8b. Change input type to " protected $_inputType = 'checkbox';" since we're
using checkboxes.
8c. Change isArray to " protected $_isArray = true;"
8d. Scroll down to where you see "$radio = '<label'" and modify it to be
"$radio = '<li><label'".
8e. Scroll a little bit farther down where you see " . '</label>';" and
change it to " . '</label></li>';"
You have no "wrapped" your elements in a "<li>" tag.
8f. Scroll down to the bottom where you see " return $xhtml;" and change it
to be:
" return "\n<ul>\n" . $xhtml . "\n</ul>\n";"
This will wrap all your elements in an unordered list.
Now, when you create your MultiCheckbox element simply change it's helper by
doing this:
$element->helper = 'formMultiCheckboxList';
When your element prints out, it should print it out in an unordered list.
In the case of the Original poster, Philip G, instead of wrapping the
element in "<li>" tags, use "".
If you need this:
"<div>
<p><label>Foo</label></span</p>
[...]
</div>"
You modify parts 8d-f as follows:
8d. "$radio = '<p><label'". "
8e. ". '</label></p>';"
8f. "return "<div>" . $xhtml . "</div>";"
You could, of course, leave off the "<div>" or "<ul>" tag and just add the
HtmlTag decorator to the element after "ViewHelper", but since this is
already a custom helper, why add an extra step and function calls/process to
your code?
I hope that helps and works. I got it working on my system pretty easily.
Although, I think I'm commiting a foul by not changing the prefix name on
the new class to something other than "Zend_View_Helper" to something like
"My_View_Helper" and then registering the path for these view helpers.
That's not something I've done yet.
It's pretty fast to make something work, but there's a lot of understanding
to be had.
Cheers!
--
View this message in context:
http://n4.nabble.com/How-do-you-decorate-MultiCheckbox-elements-tp634654p990601.html
Sent from the Zend Framework mailing list archive at Nabble.com.