Server Action | DB Update Conditions | Differentiate between empty field and no field

Hi,

I have a page with 45 input fields and want to dynamically save as soon as a field is changed.

For that, I am building a server action that updates a Database entry and I am leveraging the strong Wappler ‘condition’ functionality in order to send the SQL update only for the columns that have a field submitted.


However, I am not able to differentiate between the following two cases:
Case 1: A field was not submitted
Case 2: A field needs to be set to “” (Empty string)

In both cases, the update is ignoring the change and the condition is not applying.

Is there a way to update the field to “” if an empty field is submitted?

Thank you,
Elias

You can set a default value.
Remove the condition. And in the value part, set it as $_POST.FirstName.default('').

Thanks @sid for your input but my question is about what to put in the condition field for the above differentiation to happen. If I remove the condition like you suggested, when i post an update for the first name only for example, the rest of the fields would be lost and set to their default values

I understand your concern. A question: how are you handling the saving part? What happens on the page when a field’s value is changed?

That is not how the update record works.
If the fields are not empty and the value in the field is the same as in the database - no changes will be made. Update record only updates what’s changed.

Hi @Teodor and @sid,
What I am actually looking to do is as follow:

I have a page with around 45 input fields spread across 2 tabs. Here is a screenshot of the first 5 fields:

I am implementing a dynamic event for each field to submit an individual form as soon as the field is changed:
image
The individual form would include 2 fields only: the ID and the Field being changed only (ex: FirstName or LastName, etc.)

On the server side, I have implemented only one updateServerAction with conditions:


This is working well, however, if the user opts to edit the LastName field for example and put nothing in it, the ServerAction will not be putting “” in the LastName field. It will just assume there is no update to the LastName. How can I do correct this?

1/ Shall I fine-tune the condition to be able to do this differentiation? (Differentiation between when a field is submitted with empty value and when a field is not submitted at all)

2/or is there any other approach that you would suggest to have the updates on field change? (while avoiding the creation of 45 ServerActions of course)

Thank you for your continuous support

Updating each field on value change is a weird approach when there are 45 fields!
The current implementation will submit the COMPLETE form with 45 fields to the server connect. This is why I asked what your approach is.
In the current scenario, the solution I have given, will work fine. Because other fields will have their value as shown on the screen sent to the server when form is submitted.
So I would suggest to just give the user a “SAVE” button to avoid sending so much data on the server again and again.

Thanks @sid but I was applying @Teodor\s recommendation as per this other thread: Auto-submit form on field change

I am doing this for a couple of reasons:
1/ The user might be changing only a couple of values, and to have a beter UX, I prefer to validate and submit only what he has changed instead of having him go over the 45 fields
2/ In case the user goes through different fields and then forgets to submit, I do not want to loose everything.
Is there another way to approach this?

Sid’s suggestion for default values will work just fine here - if they edit one input and leave the rest empty, it will add the default value.
If they edit one field and leave the rest untouched, their values will remain the same.

In that case @teodor, you suggest that I submit the full form with 45 fields as soon as any field is changed?

Both questions are not really related.
You can submit the form with the dynamic event without any issue.

I thought the problem here is how to use default values, and Sid explained how to do this :slight_smile:

The problem is how to use condition field to differentiate between a field that is not submitted and a field that is submitted empty

but that’s fine, I will use as a workardound the auto-submssion of the full form at each field change event. thanks both

Because you are submitting all the fields always in current implementation, conditions do not matter. Just put default values for each field.

I cannot think of a more efficient way of doing this right now - sending just one changed field at a time… and using ONE server action for all fields. Will reply if I think of something.

2 Likes