1

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

1
  • You can perform operation on your current array to get desire one. Or else have look at zend subform. Commented Mar 18, 2013 at 9:52

2 Answers 2

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

This is a nice work around but is there a way using Zend Subforms? I tried zend subforms but can only go to one array level...
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.