Issues

ZF-3924: isValid() throws exception with deeply nested subforms

Description


    public function testAction()
    {
       $subForm1 = new Zend_Form_Subform(array('legend'=>'Subform 1'));
       $subForm2 = new Zend_Form_Subform(array('legend'=>'Subform 2'));
       $subForm3 = new Zend_Form_Subform(array('legend'=>'Subform 3'));
       
       $subForm3->addElement('checkbox', '1', array('label' => 'Checkbox'));
       $subForm3->addElement('text', '2', array('label' => 'Textbox'));
       
       $subForm2->addElement('checkbox', '1', array('label' => 'Checkbox'));
       $subForm2->addElement('text', '2', array('label' => 'Textbox'));
       
       $subForm2->addSubform($subForm3, '3');
       $subForm1->addSubForm($subForm2, '2');
       
       $form = new Zend_Form();
       $form->addSubform($subForm1, '1');
       
       $form->addElement('submit', 'submit', array('label' => 'Submit'));
       
       if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
            echo "Succeeded";
       }
       
       $this->view->form = $form;
    }

This throws the following exception:


exception 'Zend_Form_Exception' with message 'Zend_Form::Zend_Form::isValid expects an array' in /Users/Jason/Sites/asdf/library/Zend/Form.php:1973 Stack trace: 
#0 /Users/Jason/Sites/asdf/library/Zend/Form.php(1995): Zend_Form->isValid('') 
#1 /Users/Jason/Sites/asdf/library/Zend/Form.php(1993): Zend_Form->isValid(Array) 
#2 /Users/Jason/Sites/asdf/library/Zend/Form.php(1993): Zend_Form->isValid(Array) 
#3 /Users/Jason/Sites/asdf/application/modules/default/controllers/TestController.php(47): Zend_Form->isValid(Array)

Things to note:

1: This will work fine when the elements from subform 2 are removed 2: This will work fine when subform 3 is removed entirely 3: When all element names are unique and not the same name of any subform it works fine, but if you name the elements '1', '2', '3', and '4' respectively, subform 3 won't be displayed, and the form will still fail.

Comments

The problem occurs when subform names are identical with element names, so it's probably good advice to always keep subform, display group, and element names unique in a form. It would be great if there was an exception that makes clear that unique names are required.

Had the same problem and found a fix for this, having equal names for a Child Element of a SubForm and the SubForm itself should work, imho.

Fixed in [ZF-9348]

Reopened because suggested fix is not reviewed and committed yet.

Matthew Weier O'Phinney resolved [ZF-9348] Patch applied to trunk and 1.10 release branch.