ZF-12513: Adding subForm in displayGroup

Description

It should be nice to be able to render subForm in DisplayGroup from main form

Comments

Hi Angel, can you give us a use case for this? Thanks.

There is form for "Company". Company can have a lot of phones. Every phone is sub-form. All phone forms should be in one displayGroup

Use the {{setElementsBelongTo}} method for all subforms and the {{HtmlTag}} decorator to add a {{fieldset}} element:


// Form
$form = new Zend_Form();
$form->setDecorators(array('FormElements', 'Form'));
$form->setElementDecorators(array('ViewHelper', 'Label'));

// Subform 1
$subForm1 = new Zend_Form_SubForm();
$subForm1->setElementsBelongTo('phones');
$subForm1->setDecorators(array('FormElements', 'Fieldset'));
$subForm1->setElementDecorators(array('ViewHelper', 'Label'));

$subForm1->addElement(
    'text',
    'foo',
    array(
        'label' => 'Foo',
    )
);

$subForm1->addDecorator('HtmlTag', array('tag' => 'fieldset', 'openOnly' => true));

// Subform 2
$form->addSubForm($subForm1, 'foo');

$subForm2 = new Zend_Form_SubForm();
$subForm2->setElementsBelongTo('phones');
$subForm2->setDecorators(array('FormElements', 'Fieldset'));
$subForm2->setElementDecorators(array('ViewHelper', 'Label'));

$subForm2->addElement(
    'text',
    'bar',
    array(
        'label' => 'Bar',
    )
);

$subForm2->addDecorator('HtmlTag', array('tag' => 'fieldset', 'closeOnly' => true));

$form->addSubForm($subForm2, 'bar');


    Foo
            Bar
            

Thank you for the example. Display groups can be emulated this way, but * u must be sure there is no later decorator that will wrap whole form * decorators can be added only once. If i want to use htmlTag twice, i need to create new class witch extends htmlTag * i need to set order to every form


$displayGroup = new zend_form_displaygroup()
displayGroup -> addElements(subForm1, subForm2);
$form -> addDisplayGroup(displayGroup);

is much clearer than


$subForm1->addDecorator('HtmlTag', array('tag' => 'fieldset', 'openOnly' => true));
$subForm2->addDecorator('HtmlTag', array('tag' => 'fieldset', 'closeOnly' => true));
$form->addSubForm($subForm1, 'foo');
$form->addSubForm($subForm2, 'bar');

Not very powerful arguments, but think about it :)

{quote}decorators can be added only once. If i want to use htmlTag twice, i need to create new class witch extends htmlTag{quote} This is wrong!


$subForm1->addDecorator(array('foo' => 'HtmlTag'), array('tag' => 'fieldset'));
$subForm1->addDecorator(array('bar' => 'HtmlTag'), array('tag' => 'fieldset'));

yep, i didn't know that, tnx :)

This issue has been closed on Jira and moved to GitHub for issue tracking. To continue following the resolution of this issues, please visit: https://github.com/zendframework/zf1/issues/51