0

在我的表单中添加元素后,我让它们以一种奇怪的顺序呈现,在一个复选框之前获得提交按钮。希望能提供有关在哪里检查快速修复的提示。

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

所有其他使用自定义装饰器的表单都按照添加的顺序呈现元素。我没有发布装饰器,因为它很乱。希望错误出在其他地方。

4

1 回答 1

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.

于 2012-04-17T12:38:00.980 回答