Search code examples
phpzend-frameworkzend-formzend-form-elementzend-view

setting defaults for zend_form_element (when using the view syntax)


I know I can create a form multi checkbox using new Zend_Form_Element_MultiCheckbox(). I'm not using this syntax however. I'm using the form view helper syntax in the view, like so:

echo $this->formMultiCheckbox('boxes', null, null, $possible_vals_array, null);

My question is how do I, using this syntax, add an array for the values that need to be checked by default?


Solution

  • The second parameter of $this->formMultiCheckbox() should be an array of values to have checked.

    So if you your $possible_vals_array looks like this:

    $possible_vals_array = array(
        'Value A' => 'Label A',
        'Value B' => 'Label B',
        'Value C' => 'Label C',
    );
    

    ... and say you want to have values A and C checked by default, you'd pass an array like this as the second parameter:

    $checked_vals_array = array('Value A', 'Value C');
    

    So your call to the helper would look like this:

    echo $this->formMultiCheckbox(
        'boxes', $checked_vals_array, null, $possible_vals_array, null
    );