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

Issue Type: Improvement Created: 2009-01-22T08:42:22.000+0000 Last Updated: 2013-01-08T08:57:15.000+0000 Status: Resolved Fix version(s): - 1.12.2 (25/Feb/13)

Reporter: Arthur Kang (arthurkang2) Assignee: Frank Brückner (frosch) Tags: - Zend_Form

  • After1.12.0

Related issues: Attachments: - Form.php.patch


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.

<pre class="highlight">
$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


Posted by Sven Weingartner (wissenstausch) on 2010-07-14T05:11:32.000+0000

We have defined the forms as XML:

<pre class="highlight">


For this we override the addSubForms() method:

<pre class="highlight">
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);

            if (is_array($spec)) {
                $argc = count($spec);
                $order = null;
                switch ($argc) {
                    case 0:
                    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);
                        $this->addSubForm($subForm, $name, $order);
        return $this;

Posted by Frank Brückner (frosch) on 2012-07-27T14:00:37.000+0000

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

Posted by Frank Brückner (frosch) on 2013-01-08T08:57:15.000+0000

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

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.