I'm fighting with checkbox where CakePHP doesn't generate the right code and I can't understand why:
I initialize entities like that in my controller:
public $defaultPermissionFields = [
/*
* Module, item, item_visible, item_editable
*/
['item_visible' => 0],
['item_visible' => 1],
];
$permissions = $this->Permissions->newEntities($defaultPermissionFields);
So I have the following code in the ctp file:
<?= $this->Form->create($permissions, ['horizontal' => true])?>
<fieldset>
<div class="table-responsive well">
<table class="table table-bordered table-striped">
<?php foreach ($permissions as $key => $permission):?>
<tr>
<td class="text-center">
<?= $this->Form->checkbox('permissions.' . $key . '.item_visible', ['label' => false, 'value' => $permission->item_visible, 'required' => false]);?>
</td>
</tr>
<?php endforeach;?>
</table>
</div>
</fieldset>
<?= $this->Form->button(__("Save"), ["class" => "btn btn-primary btn-block"]) ?>
<?= $this->Form->end(); ?>
And strangely, the following code is generated:
<table class="table table-bordered table-striped">
<tr>
<td class="text-center">
<input type="hidden" name="permissions[0][item_visible]" value="0"/> <-- WHY ???
<input type="checkbox" name="permissions[0][item_visible]" value="0">
</td>
</tr>
<tr>
<td class="text-center">
<input type="hidden" name="permissions[1][item_visible]" value="0"/>
<input type="checkbox" name="permissions[1][item_visible]" value="1" checked="checked">
</td>
</tr>
</table>
As you can see, for the second row, the checkbox value is 1 and it's checked and it's hidden value is 0. That's the expected code.
But
For the first row, the checkbox value is 0 AND the hidden value is also 0 instead of 1.
Can you tell me why?
The second problem I have, but maybe caused by the first one is that I need to add required => false because whithout it, if I uncheck a checkbox, the browser tells me that I need to check the box !?!