简体   繁体   中英

How to get a checbox checked in cakePHP 4

So i have a checkbox and i need certain values to be checkd i tried to do this and is not working

$columnas = [
    1 => 'Columna 1',
    2 => 'Columna 2',
    3 => 'Columna 3',
    4 => 'Columna 4',
    5 => 'Columna 5',
    6 => 'Columna 6',
    7 => 'Columna 7'
];

$columnas_actuales = [
    1 => 3,
    2 => 5,
    3 => 7,    
];

<?= $this->Form->control('cliente_configuracion.columnas_visibles', [
    'options' => $columnas,
    'selected' => $columnas_actuales,
    'multiple' => 'checkbox',
    'label' => false,
    'templates' => [    
        'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
        'checkboxFormGroup' => '{{label}}',  
        'checkboxWrapper' => '<div class="form-check form-check-inline">{{label}}</div>',
    ]
])  

$columnas_actuales is the array that contains the selected options but for some reason they are not being checked.

in new in cake and in php so i dont know if im doing something wrong.

Thanks !!

Edit: I added an example of the $columnas_actuales variable.

Your data looks correct, however there is no selected option. The options to pre-select values are called default and value , where the former can be overwritten by request/context data (for example when a user changes the selection and submits the form), and the latter will always fix the selected values.

So you'd probably want to use default :

'default' => $columnas_actuales,

See also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM