Hello,
I'm new to Outsystems (I've acumulated up 20 hours of use).
I created a reactive application that consumes an API to insert or update a record in the database.
If the user enters invalid data in the inputfield, this api will return a structure list with the fieldname and the error message.
I'm creating a client action to loop through this list and show invalid fields to the user.
but I don't know if I'm doing it right.
and if it's the right way, I don't know how I can organize this in my client action. (I have 4 inputfields on this screen, but I have another one with 10. It looks like this is going to be ugly.)
I think that the perfect world would be if it were possible to loop the form inputfields and compare to the the rest api returned inputfields, but it seems that this is not possible.
Client Action Proccess Print:
The answer was found in this post.
Thank you @Ravi Punjwani, you really helped me.
@Kilian Hekhuis, because you mentioned that it was not possible, I ask you to check this post and see if I will have any problems in the future if i go this way.
Hi Thales,
If you want to return a list containing fieldname and error message, then just creat a structure with these two fields, and the variable which hold the data to return should be list of this structure type.
Now populate the error information in that list by assigning FieldName and ErrorMessage value for each error you want to return, then append more items in that list variable, using ListAppend client action. This will prepare your return variable properly, once looping through the list and all the fields is completed, you can just return it in the response.
To find out which field has error, I recommend using IF conditions rather than SWITCH, because in each loop cycle you would be able to span further in checking other fields once an error message has been added. Currently using a switch construct, it will select only one and continue with your loop. I'm not sure if this part of my suggestion will suit your requirement because I don't know your loop list variable's data looks like.
You can attach a sample app OML file to your question for further analysis based on your current scenario.
Your idea of trying with json is very good. I'm trying this with javascript together.
It looks like it will work, I'll let you know if it does.
But I don't like the idea of using multiple Ifs in the app side.
I have several Ifs in the rest api side.
I want to keep the ifs on the rest api side and automate this filling on the app side.
My goal is that eliminate the dependency on working changes on both sides.
Can you show the output JSON in case of an error? Because I'm not quite sure what you mean by "a structure list with the fieldname and the error message" - are the fields with errors sent as attributes?
"are the fields with errors sent as attributes? "
Yes, my rest api returns a list with each invalid field.
and I need to read this ScreenElement lit to fill the inputfield with the invalid message.
Ah, right, so you have on-screen input fields and want to set the pertaining Input's ValidationMessage to the message received from the REST? In that case there's little else you can do but use a Switch like you did. There's no way to iterate Inputs and dynamically assign properties or the like.