2010-09-19 94 views
2

當填充一個複選框,我有一個Zend_Form的一個複選框:在Zend中使用ViewScript裝飾形式

$horz = new Zend_Form_Element_Checkbox('horizontal'); 
$horz->setLabel('Display horizontally?'); 

$horz->setDecorators(array(array('ViewScript', array('viewScript' => 'partials/forms/checkbox.phtml')))); 

我定製viewScript checkbox.phtml看起來是這樣的:

<?php 
$name = $this->element->getName(); 
$attrs = $this->element->getAttribs(); 
    $options = $attrs['options']; 
?> 
<dt id="<?= $name ?>-element"> 
<input type="hidden" value="<?= $options['uncheckedValue'] ?>" name="<?= $name ?>" /> 
<label> 
    <input type="checkbox" class="checkbox" value="<?= $this->element->getValue() ?>" id="<?= $this->element->getId() ?>" name="<?= $name ?>"> 
    <?= $this->element->getLabel(); ?> 
</label> 
</dt> 

它呈現漂亮,但是當我從數據庫記錄填充表單時:

$record = array('horizontal'=>1); 
$form->populate($record); 

未選中該複選框。有什麼我需要在我的viewScript中設置,以允許它填充?

回答

3

填充該值後,您必須勾選HTML視圖腳本中的框。

<input type="checkbox" class="checkbox" value="<?= $this->element->getValue() ?>" id="<?= $this->element->getId() ?>" name="<?= $name ?>" <?php echo $this->element->isChecked() ? " checked=\"checked\"" : "" ?> />