ZF-3498: MultiCheckbox does not allow easily setting checked values

Description

Not sure if this is a documentation issue, or something that needs API changes. Currently, it is difficult to set default values for a multiCheckbox element. The way to do so is using:


$element->setValues(array(
    'key1',
    'key2',
    ...,
));

but it is not documented, and is unintuitive.

Comments

OK, you can check elements manually just by doing

$element->setValues($id);

now when dealing with large amounts of Options in your collection checking them like that does not work

foreach($option as $opt) $element->setValues($opt->id);

This will fail, without generating any error.. and the only way to check more than 1 is sending a whole array

Marking as a documentation improvement.

Document update applied in r10274

Hello,

I'm sorry that I am reopening the issue but I have difficulties setting up default values for a _MultiCheckbox element. I create Zend_Form_Element_MultiCheckbox with multiple options like this:

$multiCheckbox = new Zend_Form_Element_MultiCheckbox( 'elId',array ( 'disableLoadDefaultDecorators' =>true ) );

$multiCheckbox ->setName( 'elId' ) ->setLabel('elId') ->setRequired( false ) ->setAttrib('class', 'inputtext') ->setDecorators( array( 'ViewHelper' ) ) ->setMultiOptions( $options ) ;

where the $options array is an associative array 'key' => 'value'. The field is displayed just fine and I can get all the checked values for that element.

When returning to that page I need to restore from the DB the whole list of options again and mark the checked ones. I have tried to do it like that:

$multiCheckbox ->setValue( $defaults );

where $default is array, containing elements of type 'checked_option_field_id' => true. That action checks ALL the checkboxes and not only the once I need and I have passed to the setValue() method. I have tried to pass just an array containing elements of type 'checked_option_field_id', but that also doesn't work - NONE of the checkboxes is checked. I have used the form setDefaults() method with those two kinds of arrays, but the results are same - as this method uses again setValue() for each element.

MultiCheckbox element is rendered like that ( result when try to set checked value for only one option ):

BoRoom
BoRoom Eng2

That element populates the checked option values in the elId[] array. That is the element name. setDefaults() form method gets all form elements by name and commit their default values by calling setDefault() form method and after that setValue() element method. So my multicheckbox element has name elId ( it does not get all the element options one by one ) and set default values for all options instead of just the given in the array.

That is how I see it and I can't find solution how to set default values only for some of the options of a multicheckbox element.