简体   繁体   中英

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...

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM