ZF-5613: Allow subforms to be initialized at Zend_Form instantiation with config

Description

It would be a great and simple additional feature to allow subforms to be part of the Zend_Form instantiation using a config. Currently, they are "forbidden" in the Zend_Form file. I would imagine a simple loop adding new Zend_Form_SubForm would do it. Currently, I do use configs like the following, but I have a simple loop through it to add the subforms to my form in my controller.


$form_config = array(
    'attribs' => array(
        'name' => 'form_name'
    ),
    'subForms' => array(
        'subform1' => array(
            'elements' => array(
                'test1' => array('Text', array(
                    'label' => 'Test1'
                ))
            ),
            'order' => 2
        ),
        'subform2' => array(
            'elements' => array(
                'test2' => array('Text', array(
                    'label' => 'Test2'
                ))
            ),
            'order' => 1
        )
    )
);

Comments

We have defined the forms as XML:


address_form
    ...
    
... ... address

For this we override the addSubForms() method:


public function addSubForms(array $subForms)
    {
        foreach ($subForms as $key => $spec) {
            $name = null;
            if (!is_numeric($key)) {
                $name = $key;
            }

            if ($spec instanceof Zend_Form) {
                $this->addSubForm($spec, $name);
                continue;
            }

            if (is_array($spec)) {
                $argc = count($spec);
                $order = null;
                switch ($argc) {
                    case 0:
                        continue;
                    case (1 <= $argc):
                        $subForm = array_shift($spec);

                        //---------- FIX start
                        if (!$subForm instanceof Zend_Form) {
                            $subForm = new Zend_Form($subForm);
                        }
                        //---------- FIX end

                    case (2 <= $argc):
                        $name = array_shift($spec);
                    case (3 <= $argc):
                        $order = array_shift($spec);
                    default:
                        $this->addSubForm($subForm, $name, $order);
                }
            }
        }
        return $this;
    }

Patch and unit tests. (includes a extra unit test for XHTML output)

Fixed on trunk (25192) and release-1.12 (25193)