i have a form with checkbox inputs ,each with a static value of 1 , when checkboxes are checked and form is submitted it checks the fields in the database . when i uncheck and and submit the form it doesn’t uncheck the fields in the database.
20210910_162304
… the checkboxes are initially checked because i have checked attribute set on the checkboxes to check if the fields are checked from a database query.
In your server action API, where you are inserting or updating the checkbox field, add the Default formatter to it and set that to null (or 0 or whatever you’d like it to be if the checkbox isn’t checked).
If wanting it to be null, it would look like this: {{$_POST.uActive.default(null)}} but Wappler doesn’t yet have the ‘null’ value available in the UI so I type null and then remove the single quote marks.
The other way you can do this is from the client side, just if you wanted
You click the Checkbox Input element you want
remove the static value completely
scroll down to Dynamic Attributes -> Input -> value, click the dynamic data picker
choose checked as the binding, click the magic wand for data formatters
choose Ternary Operation, under ? add 1 and under : add 0
Now if the checkbox is already checked the value will be 1
if the checkbox is unchecked it will be 0 and checking it will change it to 1, while unchecking makes it 0
Its a longer method, but always best to know the server side and client side alternative for when you may need it.
Sorry I should have mentioned that, I still do the server side as Jon mentioned anyway, so I kind of do server side and client side with checkboxes, my main reason is I often am not using it like a boolean, sometimes i use checkboxes for checked = admin and unchecked = user, then i check it with a condition on the server side.
But I do some strange stuff, im sure is not even needed, haha.
actually while we on the checkbox subject, how does the checkbox know if the boolean is 1 and 0 vs true and false.
If i set the static value to true vs 1 is that enough to say to opposite of 1 is 0 and the opposite of true is false?
When you make a query and you set a boolean field as condition with value 1 or 0 and you save, if you reopen the query builder your 1 or 0 will be substituted with true or false…
I get that, but my question is what if a user particularly want the output to be true or false in their database stored records. Another user may particularly want it stored as 0 or 1, trying to see if there is some logic in the checkbox itself that reads what the value is set to, and automatically chooses the opposite.
Hi Paul, that is set in the database as the default. This can be anything like 0, 1, true, false, yes, no etc.
The true value is set by the checkbox. Like, if the database default is no, the checkbox could have a value of yes. A non-checked text box will leave the default value of no, while a checked box will change the value to yes.