Skip to content

Instantly share code, notes, and snippets.

@jonahlyn
Created April 22, 2011 21:22
Show Gist options
  • Save jonahlyn/937720 to your computer and use it in GitHub Desktop.
Save jonahlyn/937720 to your computer and use it in GitHub Desktop.
Custom render a Zend Form MultiCheckbox element with a viewscript
$audience = new Zend_Form_Element_MultiCheckbox('audience', array(
'label' => 'Target Audience',
'required' => true,
'multiOptions' => array(
'students' => 'Students',
'faculty' => 'Faculty',
'staff' => 'Staff',
'stustaf' => 'Student Employees',
'retiree' => 'Emeritus/Retiree'),
'validators' => array(
array('NotEmpty', true, array(
'messages' => array(
'isEmpty' => 'You must select at least one target audience.'
)
))
),
'decorators' => array(
'Errors',
array('ViewScript', array('viewScript'=>'multicheckboxview.php'))
)
));
<?php
$elem = $this->element;
$elemName = $elem->getName();
$values = $elem->getValue();
?>
<ul class="condensed">
<?php foreach($elem->getMultiOptions() as $option=>$value){ ?>
<li>
<input type="checkbox" name="<?php echo $elemName; ?>[]" id="<?php echo $elemName; ?>-<?php echo $option; ?>" value="<?php echo $option; ?>" <?php if($values && in_array($option, $values)){ echo ' checked="checked"'; }?> />
<label for="<?php echo $elemName; ?>-<?php echo $option; ?>">
<?php echo $value; ?>
</label>
</li>
<?php } ?>
</ul>
@jonahlyn
Copy link
Author

Glad it was useful.

@kaiohken1982
Copy link

@jli168
Copy link

jli168 commented Oct 31, 2013

thanks! I am using it now!

@jirrick
Copy link

jirrick commented Dec 26, 2015

thank you, still very useful despite being almost 2016 :)

@MarcoDeJong
Copy link

Still useful today ;) Hero!

@michelepatrassi
Copy link

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment