Anders Gunnarsson wrote:
> 
> Old subject, but did you ever solve this?
> 
> I have the same problem
> 
Given Matthew Weier O'Phinney's expertise, I'd say he's correct.  

You need to basically create a custom view helper to render the individual
elements of a multi-element.  This would be an element that implements
Zend_Form_Element_Multi, such as:

Zend_Form_Element_MultiCheckbox
Zend_Form_Element_Multiselect
Zend_Form_Element_Radio. 

This doesn't appear to be a simple feat, as I'm trying to poor over all the
code and how it all connects to each other, but here's what I gathered:

* Following any form element class all the way up and you end up at
Zend_Form_Element.

* Zend_Form_Element calls "render()" which will call each individual
decorator that has been set.

* The first decorator you're going to see for Zend_Form_Multi should be
"ViewHelper" because this is what will rendor the actual element.

* Looking at the ViewHelper.php file (/Zend/Form/Decorator/ViewHelper.php)
you will find it's "render()" method, which is pretty straight forward.  It
basically calls the given view helper that was set.

* The View Helper that gets used is set by the Element class.  
Zend_Form_Element defaults to "formText" and Zend_Form_Element_MultiCheckbox
sets it to "formMultiCheckbox".

To change the helper, you could simply do:
$element->helper = 'helperName';
or
$element->setAttrib('helper', 'viewHelperName');

* To create a custom view helper, I would then recommend looking at the
current view helpers being used.  You can find out what's currently being
used for your given element by opening up the source code for that element. 
As an example: Zend_Form_Element_MultiCheckbox open up
"/Zend/Form/Element/MultiCheckbox.php".  You will see the helper is called
"formMultiCheckbox".

Open up the file "/Zend/View/Helper/FormMultiCheckbox.php"

You will see that this class basically just extends
Zend_View_Helper_FormRadio.  So, now you'll want to open up that file: 
"/Zend/View/Helper/FormRadio.php".

Your best bet would be to copy/paste this class, rename it, and then modify
as needed.

But then, I'm not 100% sure how you make sure this custom view helper is
findable once you create it.  I imagine it's going to be similar to how you
add other custom view helpers.

But, as you can see, it's not that easy to modify.  I can't help but think
that this is a bit disappointing that these "group" form elements
(MultiCheckbox) don't behave more like display groups in that each
individual element/object in the group can have some decorators applied to
them.

Doing something like:

<ul>
<li><label><input type="checkbox"></label></li>
<li><label><input type="checkbox"></label></li>
<li><label><input type="checkbox"></label></li>
</ul>

seems to be more complicated than it should be for something I would have
thought to be a rather common user case.

It would be far easier trying to work around this limitation and just modify
your CSS to make it work since it's easy enough to get this HTML markup:

<div>
<label><input type="checkbox"></label>
<label><input type="checkbox"></label>
<label><input type="checkbox"></label>
</div>

Just treat the "<label>" elements like the list elements and hope it doesn't
require extra markup to do what you want to do with it.

Of course, I might have missed something, so there might be another way of
accomplishing something like this, but it wasn't obvious to me.
-- 
View this message in context: 
http://n4.nabble.com/How-do-you-decorate-MultiCheckbox-elements-tp634654p990561.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to