views:

348

answers:

2

Hi, 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!

+4  A: 

Hi,

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.

Pascal MARTIN
Thank you! I managed to group.But how can I set a label or a title to each group?
Granit Luzhnica
Use the setLegend-method, or pass in a 'legend'-option with the value you want to display
PatrikAkerstrand
@Granit : I edited my answer to provide an example I found from an old project of mine ;; @Machine : thanks too !
Pascal MARTIN
A: 

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' ) );
Steve
First of all you should name your class 'LSS_Form_Element_Note' as your putting it in that directory and prefix path.And after that you need to create view helper in your _View_Helper_ name: FormNoteIn that helperput function formNote($name , .....) to render this note. Then it will function.
Krishna Kant Sharma