0
votes

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!

2

2 Answers

4
votes

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.

0
votes

try adding this class

class Zend_Form_Element_Note extends Zend_Form_Element_Xhtml
{
    public $helper = 'formNote';    
}

to /Your/Own/Form/Element directory eg

$form->addPrefixPath( 'LSS_Form_Element', 'LSS/Form/Element', 'element' );

You can then add 'note' element types which just display static text like your headings. eg

$form->addElement( 'note', 'my_note', array( 'label' => 'Heading 1' ) );