Zend_Form元素以错误的顺序呈现

时间:2012-04-17 09:03:34

标签: zend-framework zend-form zend-form-element

在我的表单中添加元素后,我会以奇怪的顺序呈现它们,在一个复选框之前获取提交按钮。希望了解在哪里检查快速修复。

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?
    }
}

输出:

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

使用自定义装饰器的所有其他表单按添加顺序渲染元素。我不会发布装饰器,因为它非常混乱。希望错误在其他地方。

1 个答案:

答案 0 :(得分:1)

我尝试使用此代码重现您的问题: -

$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();

这给了我这个输出(去除装饰器)

<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">

这是你所期待的。重现问题的唯一方法是在$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();

这给了我这个输出

<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="">

这是你得到的。所以,在我看来,你必须在$otherForm中设置元素的顺序。