Is there a toBoolean() Conversion? And How to Handle Booleans in the Data Store?

I’m a bit confused about booleans in the Data Store.

I have these two variables in my scheme which are both defined as booleans:

zzz

I’ve been struggling to set is_a_test from a checkbox, and finally realised I need to use is_a_test.checked to set it consistently. So now it appears as true/false.

But is_deleted is being read from the database and is getting values of 0 or 1. I’m thinking I should be converting it to a boolean for the datastore, but there isn’t an option for that in the menu.

What is the best way to handle booleans in a data store? Should I actually define values with a boolean function as numbers to have consistency with how I work with the database? (and avoid confusing myself?).

Best wishes,
Antony.

There is no such thing as toBolean conversion.
You should not do be doing any conversion and there is nothing to worry about.

Thanks for the quick reply @Teodor!

So here is a question:

If I have a form with a checkbox:

<input name="is_a_test"  type="checkbox" dmx-bind:checked="contact.data.contact.is_a_test"  value="1">

Why is it that when assigning the value to my boolean data store field, this doesn’t work:

f_contact.i_contact_is_a_test.value.default(0)

(which is basically what I would use in a server action)

and this does work:

f_contact.i_contact_is_a_test.checked

Can you help me understand that please?

Thanks!

How do you assign it, when and where?

to my data store all_contacts

Here is the data store definition:

store

So I am wondering whether just to have a boolean items stored as numbers in the datastore for consistency…

But HOW/WHEN do you pass this checkbox value to the data store exactly? On click, on change, on submit … on some other event?

Through a Flow which is run when the Submit button is pressed on the form…

… so I am keeping the Data Store updated in parallel with the database…

How do you run the flow Anthony? On what dynamic event exactly on which element exactly?
Can you please be more specific when explaining this?
Is this on form submit event? Is it on server connect done event? Is it on button click event?

Here it is…

<button class=" button_save" id="b_save_contact" type="submit" dmx-on:click="autosave_contact.stop();flow_update_contact.run({id: h_contact_id.value.toNumber()})">{{apptext.value.400}}</button>

Am off for a bike ride now to clear my head! :slight_smile:

If you are using this as a value:

f_contact.i_contact_is_a_test.value

it will always pass the value you set to your checkbox, no matter if it is checked or not. The value of the checkbox is always the same.
So if you set a static value of 99 - it will be passed on button click always.

And this:

f_contact.i_contact_is_a_test.checked

will return true or false depending if the checkbox is checked or no.

I don’t really get why are you worried about this boolean value?

1 Like

Well… it is:

  1. Because I tried using this to assign to the data store field, which we all use in Server Actions:
f_contact.i_contact_is_a_test.value.default(0)

and that didn’t work properly. It worked when is_a_test was 1, but not 0 (or maybe 0 and 1).

and

  1. Because in the console, I have the values of two boolean fields in the data store displayed, and one is shown in boolean (true/false) and the other in digits (0/1):

zzz

So why are they showing different types of values when they are both booleans?

So I want to find a consistent way of working and to understand what is going on here.

( I feel inclined to just make the fields is_deleted and is_a_test number fields in the data store!)

Ok i think you are confusing the terms, formats, values etc. which makes you worry about things you shouldn't really worry about.

To be able to understand what i am trying to explain, you need some understanding of how the checkboxes work. So:

Yes that is true. But in the server action, you get your checkbox value (it.s $_POST.variable) in server connect after the form is submitted. Due to the nature of checkboxes and how they function (read more here) If a checkbox is unchecked when the form is submitted, there is no value submitted to the server.
When there is no value submitted to the server your database field will get an empty string. As many other users you don't want an empty string, so you use the .default(0) to add 0 to your database field, when the checkbox is not checked.
If it's checked, on form submit it will pass its value.

This is not the same on the front end. You don't have a submit event and you don't work with $_POST variables there. On the front end, you access the value of the input, and the value of your checkbox is always what you set in the value="" attribute. It does not change when the checkbox is checked or not. You can always inspect all of this, by using dmx.app.data in the console and accessing the attributes and their values for any element.

The checked attribute, however changes on the fly, once its state changes. So it returns true or false - as you already noticed.

Not sure what database field are you using to store your is_deleted value, but if it's a TINYINT(1) (as this is what people usually use for boolean values in mysql) it accepts 0 and 1 as values for false/true.

Because - check the explanation about tinyint above.

So i continue to NOT see any issue that your database returns 0 and the checked attribute on the page returns false and i don't why are you worried..

Why would you make your life harder and your code more complicated by doing:

f_contact.i_contact_is_a_test.checked ? 1 : 0

when you can just store:

f_contact.i_contact_is_a_test.checked

Hey Teodor, thanks for such a detailed explanation late in the evening! :slight_smile:

I want consistency in my data store, as the values in it will be written to sometimes with data coming from the database, and sometimes from a form.

At the moment, the database is going to assign the field is_a_test values of 0 and 1, and the form is going to assign values of true and false, and I can only predict that giving me problems when I write my next Flow which will test the value of is_a_test.

That is why I want consistency…

I also want how I use Flows and Data Stores to be as close as possible to how I use Server Actions to avoid as much confusion as possible.

I hope that helps you to understand where I am coming from!

1 Like

Ok then, you can store the “checked” value in the data store as:

f_contact.i_contact_is_a_test.checked ? 1 : 0

this will add 1 instead of true when the checkbox is checked :slight_smile: