0

I have to display the checked box as I am getting the value in $selected variable. Currently in the below scenario i have to show two checkboxes as checked, but my code is not working. How can i fix?

$checkboxarray = [
    '0' => "By Value",
    '1' => "By Quantity",
    '2' => "By Date"
];
$selected = explode(",", "0,1");
echo $this->Form->select('mo_type', $checkboxarray, array( 'selected' => $selected, 'multiple' => 'checkbox'));
Ole Haugset
  • 3,709
  • 3
  • 23
  • 44
Devendra
  • 219
  • 2
  • 22

1 Answers1

0

You can try the default instead of selected attribute,

$checkboxarray = [
    '0' => "By Value",
    '1' => "By Quantity",
    '2' => "By Date"
];
$selected = explode(",", "0,1");
echo $this->Form->select('mo_type', $checkboxarray, array( 'default' => $selected, 'multiple' => 'checkbox'));

And the proper method is to use "val" attribute

echo $this->Form->select('mo_type', $checkboxarray, array( 'val' => $selected, 'multiple' => 'checkbox'));
bikash.bilz
  • 821
  • 1
  • 13
  • 33
  • 1
    actually seems that the right way to set default values for multiple select is 'val', see [here](https://book.cakephp.org/3.0/en/views/helpers/form.html#creating-select-pickers) – arilia Jul 26 '18 at 14:53