Hi all,
I would like to understand what is the best method to handle this use case.
I have set up 4 API actions for CRUD operation without any problems.
In the case of creating a new record I would like to use a form for field validation and if everything is ok I should call the CREATE api.
is this the right approach?
I also don't know how to pass the data in JSON to the API.
Thanks for the reply,
in my case maybe it's not the best way because I have only one form to manage both the insert and the update.
The ideal would be to understand if the form is validated and call the correct api action based on the selected button (Save/Update)
In both cases I should validate the fields before calling the APIs but at the moment I don't know how to do it since I don't do the submit.
I wouldn't like to use the backend because I would have to build an API that calls other APIs and it doesn't seem very logical to me.
I would like to manage everything on the frontend side.
I also encounter another problem.
Although the name of the data passed to the API is written with a capital letter, in the payload it is all lowercase and the API gives me an error because it is case sensitive.
Normally, within your form, you will have a record id field (normally hidden).
In the case of an update this has a value as it is used for the update action.
In the case of a creation form, the ud will be empty as it has not been populated.
In your API action, simply test the value id with a conditional and call the create action if empty else the update action.
Yes this is clear to me and I have set everything in this mode.
What I can't understand is how to validate the fields since the two buttons perform the loading of the API and not the submit of the form
Hey @enriweb77, while your structure of having a seperate endpoint for each CRUD action is correct, unfortunately there's a limitation that Wappler does not fully support this. Like others have mentioned to make it this work you have to have one endpoint that handles both create and update and check with a condition if the ID was passed.
An alternative method to try if you want to keep the seperate endpoints, is to add a dynamic Action URL to the form, append the ID to the URL so you are creating a more RESTful structure and add that to your routing.
Routing:
POST https://your_url/form - route handles the create
POST https://your_url/form/:id - route handles the update as the ID is passed as a param.
Wappler fully supports this. You can simply set a dynamic action for the Server Connect form through the Dynamic Attributes to have two separate actions depending on the hidden record id value.
Thanks everyone for the suggestions, I tried both methods but in the end I chose to create an endpoint as suggested by Hyperbytes since I had to manage other actions as well