Adding subForm in displayGroup #51

Closed
zfbot opened this Issue Apr 5, 2013 · 7 comments

Projects

None yet

2 participants

zfbot commented Apr 5, 2013

Jira Information

Original Issue:ZF-12513
Issue Type:New Feature
Reporter:Angel K
Created:01/25/13
Assignee:
Components:Zend_Form

Description

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

zfbot commented Apr 5, 2013

(Originally posted by: froschdesign on 01/25/13)

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

zfbot commented Apr 5, 2013

(Originally posted by: Angel K on 01/25/13)

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

zfbot commented Apr 5, 2013

(Originally posted by: froschdesign on 01/25/13)

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');
<form enctype="application/x-www-form-urlencoded" action="" method="post">
    <fieldset>
        <fieldset id="fieldset-foo">
            <label for="phones-foo" class="optional">Foo</label>
            <input type="text" name="phones[foo]" id="phones-foo" value="" />
        </fieldset>
        <fieldset id="fieldset-bar">
            <label for="phones-bar" class="optional">Bar</label>
            <input type="text" name="phones[bar]" id="phones-bar" value="" />
        </fieldset>
    </fieldset>
</form>
zfbot commented Apr 5, 2013

(Originally posted by: Angel K on 01/28/13)

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 :)

zfbot commented Apr 5, 2013

(Originally posted by: froschdesign on 01/28/13)

{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'));
zfbot commented Apr 5, 2013

(Originally posted by: Angel K on 01/30/13)

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

zfbot commented Apr 5, 2013

This issue was ported from the ZF2 Jira Issue Tracker at
http://framework.zend.com/issues/browse/ZF-12513

Known GitHub users mentioned in the original message or comment:
@froschdesign

@froschdesign froschdesign was assigned Apr 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment