Issue with checkbox label in CakePHP 4.2.8

I am using this code for checkbox

echo $this->Form->input(‘done’, [‘type’ => ‘checkbox’, ‘label’=>‘Yes, I agree to receive updates’,‘value’ => 1]);

but in form I am getting this html code .

Why label is as attribute of input instead of tag.
what I am missing?

What you are looking for is $this->Form->control() which outputs labels as well as inputs.

The function $this->Form->input() can only be used to print anything ONLY related to <input>.

So either use

$this->Form->control(('done', ['type' => 'checkbox', 'label'=>'Yes, I agree to receive updates','value' => 1]);

or

echo $this->Form->label('done', 'Yes, I agree to receive updates');
echo $this->Form->input('done', ['type' => 'checkbox','value' => 1]);
1 Like

@KevinPfeifer Thank you for this solution, but in my context the changed value of the checkbox was not saved.
After modifying your code, the changed value of the checkbox was saved:

echo $this->Form->hidden('done',['value'=>0]);
echo $this->Form->label('done', 'Yes, I agree to receive updates');
echo $this->Form->input('done', ['type' => 'checkbox']);

Maybe this will be helpful for someone.

You can use ‘hiddenField’ => true. it will create hidden field.

Thanks