Zend_Form无法在循环中创建元素

时间:2012-03-23 17:40:12

标签: zend-framework zend-form

就像在主题中我尝试在元素循环创建时创建表单:

foreach($aQuestionList as $aQuestionValue){

        $aAnswerList = $oAnswerList->getListByQuestionId($aQuestionValue['newsletter_question_id']);

        $oNote = new SilverCms_Core_Form_Element_Note('note',array('value'=>'<p>'.$aQuestionValue['newsletter_question_name'].'</p>'));
        $this->addElement($oNote);

        foreach($aAnswerList as $aAnswerValue){
            $oCheckBox = new Zend_Form_Element_Checkbox('answer');
            $oCheckBox->setLabel($aAnswerValue['newsletter_question_answer_name']);
            $oCheckBox->isArray(true);
            $oCheckBox->setCheckedValue($aAnswerValue['newsletter_question_answer_id']);
            $this->addElement($oCheckBox);
        }
    }

但是当我想在视图中显示表格时

<?php echo $this->filterform;?>

它只显示数组aQuestionList中的最后一个元素,同样的情况是在第二个循环中 最诚挚的问候

1 个答案:

答案 0 :(得分:4)

您为每个答案复选框使用相同的名称(“答案”),因此每个答案都会覆盖前一个答案。我想你真的想要Multicheckbox表单元素:

$answer = new Zend_Form_Element_MultiCheckbox('answer');
foreach($aAnswerList as $aAnswerValue){
    $answer->addMultiOption($aAnswerValue['newsletter_question_answer_id'], $aAnswerValue['newsletter_question_answer_name']);
}
$this->addElement($answer);

这将为每个选项提供一个复选框和标签。