display checked input field in cakephp 3

99 Views Asked by At

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'));
1

There are 1 best solutions below

1
On

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'));