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

Issue Type: Docs: Improvement Created: 2008-06-23T10:58:05.000+0000 Last Updated: 2009-09-29T08:02:49.000+0000 Status: Resolved Fix version(s): - 1.5.3 (28/Jul/08)

Reporter: Matthew Weier O'Phinney (matthew) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: Attachments:


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:

<pre class="highlight">

but it is not documented, and is unintuitive.


Posted by Hertzel Armengol (emudojo) on 2008-07-18T09:04:29.000+0000

OK, you can check elements manually just by doing


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

Posted by Matthew Weier O'Phinney (matthew) on 2008-07-22T06:42:06.000+0000

Marking as a documentation improvement.

Posted by Matthew Weier O'Phinney (matthew) on 2008-07-22T08:10:22.000+0000

Document update applied in r10274

Posted by Dessislava Mitova (dessislava) on 2009-09-29T08:02:49.000+0000


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

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.