I think you need to add this to your form: $form->setIsArray(true);

If you then access (that's what I do) the form values in a controller like that:
$this->getRequest()->getPost('arr')
...you should get the expected array.

Just take care that you have to set the form name to match the
expected array name. I use the constructor for that:
$form = new Zend_Form(array ('name' =>'arr'));

Jules Piccotti

2009/10/22 Krzysztof Szatanik <[email protected]>:
> Hi
>
>   I have some issues with Zend_Form and getting values as arrays.
>
>   For example - i have 2 elements - e1 and e2 and i want them as array
> (arr):
> Something like this:
>
>   [arr] => Array
>       (
>           [e1] => val1
>           [e2] => val2
>       )
>
>
>   Closest form design i can get is this code:
>
>       $form = new Zend_Form();
>             $form->addElement( 'text', 'e1', array(
>           'belongsTo' => 'arr',
>           'label'     => 'e1',
>       ));
>             $form->addElement( 'text', 'e2', array(
>           'belongsTo' => 'arr',
>           'label'     => 'e2',
>       ));
>             $form->addElement( 'submit', 'submit', array());
>                   $form->setDefaults($this->getRequest()->getParams());
>             echo '<pre>Form values: '; print_r( $form->getValues() ); echo
> '</pre>';
>       echo '<pre>Request params: '; print_r(
> $this->getRequest()->getParams() ); echo '</pre>';
>             echo $form;
>
>   But, there is still huge problem: Sample output:
>
> Form values: Array
> (
>   [e1] =>   [e2] =>   [submit] => submit
> )
>
> Request params: Array
> (
>   [controller] => test
>   [action] => index
>   [module] => default
>   [arr] => Array
>       (
>           [e1] => val1
>           [e2] => val2
>       )
>
>   [submit] => submit
> )
>
> As you can see even if i get form values in request as i wanted, they aren't
> recognized by Zend_Form. Output of $form->getValues() is different from
> expected, and even fields aren't recognized, so i can use Zend_Form only to
> generate HTML form but not to filter and validate submited form.
>
> I tried in many ways using different combination of isArray, belongsTo, name
> and nothing works...
>
> Any solutions to this problem that i can still use Zend_Form booth to
> generate form and then to filter/validate submitted data?
>
> --
> Best Regards,
> KS
>

Reply via email to