问题
My Edit view file has a set of checkboxes. I have managed to retrieve the selected checkbox values from the database, and want to mark these as selected in the view file.
Edit view file
<div class="col-md-12">
<?= $this->Form->label('category','Pick Categories');?>
<?= $this->Form->select('category', $options,['multiple'=>'checkbox', 'required'=>'false', 'label'=>'Category','class'=>'col-md-12','selected'=>$catSel]); ?>
</div>
$options is
$options = ['A'=>'Val1',
'B'=>'Val2',
'C'=>'Val3',
'D'=>'Val4',
'E'=>'Val5'];
$catSel has been set in my controller and is returning the correct values. I checked with print_r(), shown below:
Array ( [0] => 1 [1] => 3 )
I have also tried directly entering the selected value
<?= $this->Form->select('shop_category', $options,['multiple'=>'checkbox', 'required'=>'false', 'label'=>'Shop Category','class'=>'col-md-12','selected'=>[1,3]]); ?>
I have also tried sending in $catSel as
Array ( [0] => A [1] => C )
None of this is working. Not sure why. I haven't been able to find any solution to this anywhere, apart from the set 'selected' as array of selections. Any help will be appreciated.
回答1:
The option isn't called selected
anymore, but val
. Looks like the Cookbook is a little out of date on that one, it's however shown correctly in the API docs.
Also note that you must use the actual keys of the options that you want to be selected, ie in your case you must use the latter of your examples and pass the strings A
and C
.
来源:https://stackoverflow.com/questions/28991589/cakephp-3-unable-to-mark-multiple-checkboxes-selected