views:

78

answers:

1

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 ?>-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?

+1  A: 

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\"" : "" ?> />
Esteban Petrovich