Checkbox - Please check this box if you want to proceed

CakePHP 4

In my database I have a required tinyint field that stores a 0 for false or a 1 for true. On my edit form I use a Form->checkbox(‘deleted’). There is an automatic hiddenField with a default value of zero. Therefore, if I check the box I get a 1 and if I un-check the box I should get a 0 but instead I get a message when I submit the form that says “Please check this box if you want to proceed.”

The HTML output is adding a required tag parameter which I believe is causing the issue.

How can I get the form to accept an un-checked checkbox with a value of 0

<input type="hidden" name="deleted" value="0"/><input type="checkbox" name="deleted" value="1" required="required" checked="checked">

Does setting the ‘default’ attribute to 0 work?

The database already has the default = 0 for the column. Adding a new record is not an issue. But if I edit a record, check the box, submit the form, the database field is set to 1. Good so far. Now if I go back in to edit and try to un-check the field, I get the message “Please check this box if you want to proceed.” The form does not submit. I can only check the field and submit the form.

So, you have a box that says you have to check it in order to proceed, and you’re complaining that if you don’t check it, it won’t let you proceed? This seems like it’s behaving as designed?

If you really need it to accept a “must be checked to proceed” box without it being checked, it seems like the place this is most likely coming from is your validation, which isn’t shown here.

I don’t know if I have a validation. I did not add one in the code. Where would that be?

Maybe it was added for me since the database column is a required field. But 0 and 1 are values. So the requirement should have been met.

The view has $this->Form->checkbox(‘deleted’);

What I want is the form to return a 0 if the checkbox is not checked and a 1 if it is checked. How do I accomplish that?

Ah, this is one of those built-in browser things that completely prevent the form from submitting, not an error message that appears when the page is refreshed after submitting? Can’t remember off the top of my head how to get rid of the “required” attribute that Cake puts on things, that’s presumably what’s causing this. Alternately, look up the “novalidate” option in HTML.