nested forms in ZF2

For programming and general questions on Zend Framework

nested forms in ZF2

Postby zensys on Fri Jan 25, 2013 5:09 pm

I have a form consisting of various rows each containing a subform:

Code: Select all
class Entries extends Form
    public function __construct($number = 1)
        for($i = 1; $i <= $number; $i++) {
            $row = new FormRow();
            $row->setName('row['. $i . ']');
        // more elements

        $this->prepare();      // prefixes the subform name to each element name 

That gives me nested forms with the name 'row[1]', 'row[2]' etc. and a data array:

Code: Select all
    'normal_field' => 'foo',
    'row' => array(
        '1' => array(
            'key1' => 'bar1',
        '2' => array(
            'key1' => 'bar2',

The result is that my form does not validate: the getPost() method recognizes my subforms as an array of subforms and parsed the data accordingly. But the inputfilter still uses the 'flat' form names ('row[1]') and thus the setData() method of InputFilter cannot match the keys of the data array with the keys of the subforms in the InputFilter object.

The validation problem dispappears when I use 'row1' etc. as subform names but then I cannot access the data of my subforms separately.

Is it possible to get my subform data as a nested array and still be able to validate it? The best would be to also have the subforms as an array in the Form object to be able to loop through them in my view script (e.g. to apply a fieldset tag to each of them).
Posts: 23
Joined: Mon Jun 21, 2010 11:19 am

Return to Zend Framework

Who is online

Users browsing this forum: No registered users and 10 guests