0
votes

After adding elements to my form, I get them rendered in a weird order, getting the submit button before one checkbox. Would appreciate hints on where to check for a quick fix.

class SomeForm extends My_Form {
    public function init() {
        $this->addElements();
        //add a few elements (#1)

        $this->addElements($otherForm->getElements());
        //borrow some elements from another form (#2)

        if ($trueCondition=true) {
            $this->addElements();
            //add one more element which will render at end of form (#3)
        }

        $this->addElements();
        //some more, including submit button (#4)

        parent::init();
        //call My_Form to register custom decorator; culprit?
    }
}

Output:

<inputs from addElements() #1 />
<inputs from addElements() #2 />
<inputs from addElements() #4 />   <-
<inputs from addElements() #3 />   <- mixed up order

All other forms using the custom decorator render elements in the order that they were added. I'm not posting the decorator as it's pretty messy. Hopefully the error lies elsewhere.

1
Elements are supposed to be ordered according to when you add them to the form. Maybe you're using setOrder() somewhere? - Liyali
Nope. Checked form, controller and view code, no match for setOrder - bububaba
Are you using ZS/Eclipse? If I were you I would run the form (with few breakpoints) and check what happen in a Debug perspective mode. It's usually a good (and fast) way to see how your code is executing. - Liyali
It would help to see some code from My_Form, such as the addElements() method. - vascowhite
@Liyali - I'm not using ZS/Eclipse, but I've echoed "1", "2", "3" and "4" below every addElements() and they echo in order. @vascowhite - My_Form only extends init(), isValid() and render() (the latter because of custom decorator; it just renders value instead of label on the button and translates if translation available). addElements() is called for parent class, which is vanilla Zend_Form - bububaba

1 Answers

1
votes

I tried to reproduce your problem using this code:-

$formA = new Zend_Form();
$formB = new Zend_Form();
$formA->addElement(new Zend_Form_Element_Text('formA_text1'));
$formA->addElement(new Zend_Form_Element_Text('formA_text2'));
$formB->addElement(new Zend_Form_Element_Text('formB_text1'));
$formB->addElement(new Zend_Form_Element_Text('formB_text2'));

$formA->addElements($formB->getElements());
$formA->addElement(new Zend_Form_Element_Text('formA_text3'));
$formA->addElement(new Zend_Form_Element_Submit('submit'));
echo $formA->render();

Which gave me this output (decorators removed)

<input type="text" name="formA_text1" id="formA_text1" value="">
<input type="text" name="formA_text2" id="formA_text2" value="">
<input type="text" name="formB_text1" id="formB_text1" value="">
<input type="text" name="formB_text2" id="formB_text2" value="">
<input type="text" name="formA_text3" id="formA_text3" value="">
<input type="submit" name="submit" id="submit" value="submit">

Which is what you are expecting. The only way to reproduce your problem was by setting the order of an element in $formB

$formA = new Zend_Form();
$formB = new Zend_Form();
$formA->addElement(new Zend_Form_Element_Text('formA_text1'));
$formA->addElement(new Zend_Form_Element_Text('formA_text2'));
$formB->addElement(new Zend_Form_Element_Text('formB_text1'));
$formB->addElement(new Zend_Form_Element_Text('formB_text2'));
$formB->getElement('formB_text2')->setOrder(10);
$formA->addElements($formB->getElements());
$formA->addElement(new Zend_Form_Element_Text('formA_text3'));
$formA->addElement(new Zend_Form_Element_Submit('submit'));
echo $formA->render();

Which gave me this output

<input type="text" name="formA_text1" id="formA_text1" value="">
<input type="text" name="formA_text2" id="formA_text2" value="">
<input type="text" name="formB_text1" id="formB_text1" value="">
<input type="text" name="formA_text3" id="formA_text3" value="">
<input type="submit" name="submit" id="submit" value="submit">
<input type="text" name="formB_text2" id="formB_text2" value="">

Which is what you are getting. So, it seems to me that you must be setting the order of elements in $otherForm.