Jay Bhatt
Jay Bhatt

Reputation: 5651

Zend Framework Multidimensional Form Array

I need to create a multidimensional array form using zend framework. When I post the form I should be able to get the following result as post.

Array
(
    [Address] => Array
        (
            [customer] => Array
                (
                    [name] => Customer Name
                )

            [guest] => Array
                (
                    [name] => Guest Name
                )

        )

)

For some reason I am not able to get the above result. So far the result I get is this.

Array
(
     [customer_name] => Customer Name
     [guest_name] => Guest Name
)

So my question is does Zend_Form support multidimensional form array? If yes how?

Thanks in advance...

Upvotes: 1

Views: 1141

Answers (2)

Moonchild
Moonchild

Reputation: 1562

The setName() method of Zend_Form_Element's is filtered and doesn't allow [ and ].

The setBelongsTo() method is just made for that. But i'm not shure that the setName() and setBelongsTo() couple can handle more than one dimension.

Upvotes: 0

JoDev
JoDev

Reputation: 6873

This issu isn't about ZF, but about <form> system. To retrieve a multidimensional form array, you have to provide good name attributes.

In ZF, to manipulate the name, you can use :

$form->myelement->setAttrib('name', 'myname'); or $form->myelement->setName('myname');

And to do what you expect, you have to use naming form like :

$form->element1->setName('[address][customer][name]');
$form->element2->setName('[address][guest][name]');

With this naming, you'll be able to retrieve your POST data in a multidimensional array.

Using subforms, you only can manipulate first dimension. So elements would have a naming form like :

$subform1->setName('customer');
$subform1->element1->setName('[address][name]');
[...]//do it for each element

$subform2->setName('guest');
$subform2->element1->setName('[address][name]');
[...]//do it for each element

Upvotes: 5

Related Questions