R – Zend_form rendering problem

decoratorformattingrenderingzend-formzend-framework

I need a zend_form which will contain mostly checkboxes. I need to group them and also display a title for each group. e.g.

Heading 1

Label1 Check1

Label2 Check2

Label3 Check3

Heading 2

Label4 Check4

Label5 Check5

Label6 Check6

First I don't know how to display the title ("headings")! Is there a way that you can add a label to a form bu not to an element or any other solution to add these titles in Zend Form?

Second how do I render this way? what kind of decorator should I use?
I red something about decorators but I did not understand much of it?

Anybody any Idea?
thanx!

Best Answer

You might be interested by this section of the manual, which seems to be exactly about what you want to obtain : 23.4.3. Display Groups (quoting) :

Display groups are a way to create virtual groupings of elements for display purposes. All elements remain accessible by name in the form, but when iterating over the form or rendering, any elements in a display group are rendered together. The most common use case for this is for grouping elements in fieldsets.

This should allow you to have your form elements re-grouped in fieldsets, and each one of those can have its legend -- in your case, that would be "heading X".

Once your checkboxes have been added to the form, you should be able to re-group them by using something like this :

$form->addDisplayGroup(array('checkbox1', 'checkbox2', 'checkbox3'), 'firstgroup');
$form->addDisplayGroup(array('checkbox4', 'checkbox5', 'checkbox6'), 'fsecondgroup');


For the rendering part, I suppose it'll use Zend_Form_Decorator_Fieldset


Edit after the comment

To set the title of each group, you have to set its "legend", passing that as an option.

For example, here is a piece of code I found from an old project I worked on quite some time ago :

$form->addDisplayGroup(array(
    'idCategory',
    'date',
    // ...
    'tags', 
    'nbComments'
), 
'postmeta', 
array(
    'order' => 2,
    'attribs' => array(
        'class' => 'group',
        'legend' => 'Meta-données'
    )
));

The "Meta-données", from what I recall, is the "legend" used for the fieldset containing those elements.

Related Topic