Zend_Form_Element_Checkbox多个值

时间:2011-10-24 15:12:19

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

我有以下代码

foreach ($this->getChitsInstallment() as $key => $installment) {
            $checkElement = new Zend_Form_Element_Checkbox("installment[]");
            $checkElement->setAttrib('id', 'installment_'.$key)->setDecorators($decorators->elementDecorators);
            $this->addElement($checkElement); 
            $checkElements[] = $checkElement->getName();

            $textElement = new Zend_Form_Element_Text("installmentvalue[$key]");
            $textElement->setAttrib('readonly', 'true')->setAttrib('class', 'inp-form');
            $textElement->setAttrib('id', 'text_'.$key)->setDecorators($decorators->elementDecorators); 
            $textElement->setValue($installment);
            $textElement->setLabel("Installment $key: ");
            $this->addElement($textElement);
            $textElements[] = $textElement->getName();

        }

我想要输出

<input type="checkbox" name="installment[]" id="installment_1" />
<input type="checkbox" name="installment[]" id="installment_2" />
<input type="checkbox" name="installment[]" id="installment_3" />

在视图中,我只需拨打<?php echo $this->form ?>

请给我一个解决方案

此致 Nisanth

1 个答案:

答案 0 :(得分:2)

您需要使用Zend_Form_Element_MultiCheckbox代替:

$element = new Zend_Form_Element_MultiCheckbox('installment');
foreach ($this->getChitsInstallment() as $key => $installment) {
    $element->addMultiOption($installment, "Installment $key: ");
}
$this->addElement($element);