Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework : Setting up default values for part of the multicheckbox element options not possible

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.

like image 484
Dessislava Mitova Avatar asked Dec 18 '22 05:12

Dessislava Mitova


1 Answers

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 Zend_View_Helper_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->setMultiOptions(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?

like image 93
Tim Lytle Avatar answered May 02 '23 20:05

Tim Lytle