Checkbox -> Checked for multiple select input for belongs to many associations [Solved]


#1

Hello CakePHP Community,

is there a native Cake 3.x way to Pre-Check all ._ids ?

https://book.cakephp.org/3.0/en/views/helpers/form.html#creating-inputs-for-associated-data

echo $this->Form->control('tags._ids', [
'type' => 'select',
'multiple' => true,
'options' => $tagList,
]);

In my case I use the ‘multiple’ => ‘checkbox’ option.

How could we pass a ‘checked’ => true option to this form control method?


#2
$tagList = [1 => 'one', 2 => 'two'];
echo $this->Form->control('tags._ids', [
    'type' => 'select',
    'multiple' => true,
    'options' => $tagList,
    'default' => array_keys($tagList)
]);

default is when there is no data from entity in ->Form->create() or you can use value if you always want same set


#3

You are my Man Bro! - TY

array_keys() is the Trick, but beware the $tagList must be ->toArray(); many ppl forget to change it in the controller action.

The value way is a bit too hotrod - default is nice and clean.

:v: