Populate a checkbox when using a viewScript decorator in Zend Form
I have a zend_form with a checkbox:
$horz = new Zend_Form_Element_Checkbox('horizontal');
$horz->setLabel('Display horizontally?');
$horz->setDecorators(array(array('ViewScript', array('viewScript' => 'partials/forms/checkbox.phtml'))));
My custom viewScript checkbox.phtml looks like this:
<?php
$name = $this->element->getName();
$attrs = $this->element->getAttribs();
$options = $attrs['options'];
?>
<dt id="<?= $name ?开发者_JS百科>-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>
It renders beautifully, but when I populate the form from a database record:
$record = array('horizontal'=>1);
$form->populate($record);
The checkbox is not checked. Is there something I need to set in my viewScript to allow it to populate?
You have to check the box in your html view script after the value is populated.
<input type="checkbox" class="checkbox" value="<?= $this->element->getValue() ?>" id="<?= $this->element->getId() ?>" name="<?= $name ?>" <?php echo $this->element->isChecked() ? " checked=\"checked\"" : "" ?> />
精彩评论