Issues

ZF-8078: Zend_Form::getValues() does not return all the subforms

Issue Type: Bug Created: 2009-10-16T08:11:00.000+0000 Last Updated: 2010-04-16T09:48:51.000+0000 Status: Resolved Fix version(s): Reporter: Avi Block (blocka) Assignee: Christian Albrecht (alab) Tags: - Zend_Form

Related issues: - ZF-5222

Attachments:

Description

My goal is to get my data back to me in a form like this:

<pre class="highlight">
raffle[id]
raffle[tickets][1][cost]
raffle[tickets][2][cost]

To that end, I have a form with two subforms created as such:

<pre class="highlight"> 
$form = new Zend_Form;
$form->addElement('hidden', 'id');
$form->setElementsBelongTo('raffle');
$form->addElement($element);
     
$form->addElement('submit', 'Submit');

$sf = new Zend_Form_SubForm();
$sf->addElement('text', 'cost');
$sf->setElementsBelongTo('tickets[1]');
$form->addSubForm($sf,'tickets[1]');
$sf2 = new Zend_Form_SubForm();
$sf2->addElement('text', 'cost');
$sf2->setElementsBelongTo('tickets[2]');
$form->addSubForm($sf2,'tickets[2]');

This appears to give me what I want, but in the output of $form->getValues, only the data from the second subform appears.

Comments

Posted by Avi Block (blocka) on 2009-10-16T08:19:06.000+0000

Might I suggest changing

<pre class="highlight">
foreach ($this->getSubForms() as $key => $subForm) {
            $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
            $values = array_merge($values, $fValues);
        }

to

<pre class="highlight">
foreach ($this->getSubForms() as $key => $subForm) {
            $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
            $values = array_merge_recursive($values, $fValues);
        }

Posted by Avi Block (blocka) on 2009-10-25T16:28:14.000+0000

This seems like the same issue. Please fix this.

Posted by Tom Graham (noginn) on 2009-10-27T06:06:59.000+0000

I searched but found no other open issues for this, must not have searched enough :)

Same issue I was having, and also the same fix.

Posted by Avi Block (blocka) on 2009-12-16T10:52:49.000+0000

Can we at least have a comment on this? We've gone through two bug hunts and three minor releases since I reported this bug!

Posted by Csiszár Attila (csiszarattila) on 2009-12-21T12:00:27.000+0000

I now that my comment is not related to the issue, but you can archive the desired result with nested subforms. Like this:

//Create tickets as individual subforms: $first_ticket = new Zend_Form_SubForm(); $first_ticket->addElement("text", "cost", ...); $first_ticket->addElement("text", "type", ...);

//then bind those indivudual Subforms into an another subform, using numbers as names

$Tickets = new Zend_Form_SubForm(); $Tickets->addSubForm($first_ticket, "1"); // that makes [1][cost] [1][type] in html $Tickets->addSubForm($second_ticket, "2"); // that makes [2][cost] [2][type] in html

// Then add this Subform - which holds ticket as an array of individual subforms - // to your basic form with name "tickets"

$form = new Zend_Form();

$form->addSubForm($Tickets, "tickets");

// so you have elements with ticket[1][cost] name attrib on form fields.

It might require a lot more programming than your solution, but it works, and Subforms also makes possible to validate and handle your elements more easily.

Posted by Michael Rehbein (tech13) on 2010-02-17T14:31:31.000+0000

Since raffle[tickets] is an array, it should be a subform, such as this:

<pre class="highlight">
$form = new Zend_Form;
$form->addElement('hidden', 'id');
$form->setElementsBelongTo('raffle');

$form->addElement('submit', 'Submit');

$sfTickets = new Zend_Form_SubForm();

$sfCost = new Zend_Form_SubForm();
$sfCost->addElement('text', 'cost');

$sfTickets->addSubForm($sfCost, '1');

$sfCost = new Zend_Form_SubForm();
$sfCost->addElement('text', 'cost');

$sfTickets->addSubForm($sfCost, '2');

$form->addSubForm($sfTickets,'tickets');

print_r($form->getValues());

outputs

<pre class="highlight">
Array
(
    [raffle] => Array
        (
            [id] => 
            [tickets] => Array
                (
                    [0] => Array
                        (
                            [cost] => 
                        )

                    [1] => Array
                        (
                            [cost] => 
                        )

                )

        )

)

Which is a bit closer to your desired structure, however is a zero-based array, rather then one-based.

Posted by Christian Albrecht (alab) on 2010-03-08T14:21:42.000+0000

@Avi this is not a correct solution as $fValues = $this->_attachToArray($subForm->getValues(true) is recursive anyway and merge_recursive does not add anything.

You might check if ZF-9350 resolves your issue.

Posted by Avi Block (blocka) on 2010-03-08T17:56:14.000+0000

@Christian I haven't tried your patches, but it looks like I may have been tackling the symptom of a greater problem. Good work! My patch does, however, work for the specific situation that I had.

Posted by Christian Albrecht (alab) on 2010-03-25T13:24:00.000+0000

Reopened because suggested fix is not reviewed and committed yet.

Hey Avi, i was a little fast with my Statement, indeed array_merge_recursive solves this issue. The rest is done with ZF-9586.

Posted by Christian Albrecht (alab) on 2010-04-16T09:48:51.000+0000

Resolving as duplicate of ZF-9586

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts