Unable to create a function that turns form values from empty strings into nulls

I am trying to pass empty date fields from my form as nulls. I see that this is a pretty big issue that for some reason still has not been solved. So when nothing is selected for a date field, Retool automatically turns it into an empty string. Since my database accepts only dates or nulls as a valid input, it throws errors.

I tried adding into the custom validation rules to the input

{{ etdInput3.value === " " ? null : etdInput3.value }}

However, I don't think I am able to set values in the validation box.

Then I tried adding custom JS code, which is triggered on the submit button, which on success triggers the update query to the table. This however does not change anything inside the form1 object.

I am trying to figure out a way how I can only isolate the date fields as I have over 50 keys in the form. Any help is appreciated.

if (form1.data.ata_delivery === "")
  form1.data.ata_delivery = null
if (form1.data.eta_delivery === "")
  form1.data.eta_delivery = null

Hey there @Alen_Ramic and welcome to the forum.

So, the way I usually approach this is to manipulate the changesetObject within my insert query, i do something like:

{{ ({ ...form8.data, delivery: date1.value === "" ? null : date1.value }) }}

In the same you can define all of your date values.

Yea i painfully added over 50 elements to the key value pairs and ran code like you mentioned above. It worked.

For integers, the forms has an Allow Null option under validation. It would be very convenient if there was a similar option for dates. It seems as though alot of people are running into the same issue.

Thanks for your help

2 Likes