views:

2049

answers:

3

Hello,

I'm writing this question cause I have difficulties setting up default values for a _MultiCheckbox element of a Zend Framework 1.9.3. 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
(eg. array( '1222' => true, '1443' => 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',
(eg. array( '1222', '1443' ) )
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 ):

<label for="elId-1222"><input type="checkbox" name="elId[]" id="elId-1222" value="1222" checked="checked" class="inputtext">BoRoom </label><br />

<label for="elId-1443"><input type="checkbox" name="elId[]" id="elId-1443" value="1443" checked="checked" class="inputtext">BoRoom Eng2 </label><br/>

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.

A: 

The setValue() expects a array with those values that need to be checked, in this case for example you need to pass a array with values 1222, 1443 for them to be marked as checked.

Chris
I have mentioned above that passing such array also doesn't work - none of the option elements is checked.Passing array of kind array('1222', '1444') does not check any checkbox.Passing array ('1222' => true, '1444' => true) checks all checkboxes but not only those specified in the array.Also, i don't find at all proper logic in the Form Element setValue() method that will set default values to multi element.Anyway, I also begin to think that the problem may come because of the HtmlTag decorator or the ViewHelper decorator which I use to render the field and the way it does it.
Dessislava Mitova
+2  A: 

Chris is correct that setValue() expects an array of values to be 'checked' (not an array of bool values keyed by your option IDs).

If you are looking for the logic behind the form generation, don't look at the Zend_Form_Element object (or the many extended elements from it), look at the Zend_View_Helper objects. Specifically the _FormRadio object.

When generating the HTML the options array is looped, then the value is checked against the value array - the array passed to setValue(), using in_array().

From Zend_View_Helper_FormRadio (), line: 150

// is it checked?
$checked = '';
if (in_array($opt_value, $value)) {
    $checked = ' checked="checked"';
}

Not sure what that's not working for you, but if you're passing:

$element->setMultiOpitons(array('1111' => 'Some Label', 
                                '2222' 'Some Other Label', 
                                '3333', 'Not Selected Label'));

$element->setValue(array('1111','2222');

It should work. Maybe if you could include some code it would be easier to see what's going on?

Tim Lytle
You're both right. I managed to make it work passing an array with option values. Thank you for the help.
Dessislava Mitova
A: 

Hello,

You need to serialize the checkbox value before insert in database. To show the database value selected again you have to unserialize the data to show.

The details you can read from following link

http://abser-web-tips.blogspot.com/2010/09/zend-framework-multiple-check-box.html

Thanks

abser