How do I by default disable a checkbox but still get this to work?

OK, I know this is probably a basic question but check boxes are still driving me nuts.

I have a single custom checkbox and a corresponding boolean field in the database - checkbox set to the following:

On form submit it works as expect when you check and uncheck the checkbox it stores the correct 0 and 1 in the boolean in the database.

But, I have tried setting the static value to 0 to uncheck the checkbox by default and it no longer adds the correct value to the database when you check it.

What am I doing wrong??

The outcome I want is on form load the checkbox is unchecked, but still add the correct boolean value into the database when you check it is 1 and unchecked is 0.

Under Dynamic Attributes you could add a default value of 0?

Then it should be 0 (unchecked by default) and as you already have it would be 1 if checked.

1 Like

Just a bit of error in how checkboxes work — the value has nothing to do with the box being check or not; that is handled by the CHECKED attribute. The value is simply what is sent IF the box is checked. If the box is not checked then nothing is sent at all. That is where using a default value on the server side comes in. You set a value if nothing is sent.

1 Like

thanks @brad & @mebeingken

This worked a treat and I think I know why :thinking:

Cheers

1 Like

On your database insert you can do something like value ? '1' : '0'

Actually you simply need to add the default formatter and make the default value 0:

so when the checkbox is not checked it will insert 0 and when checked it will insert its value (the static value entered)

1 Like