< Articles

Tech Bites - Should we validate form data on server-side?

Hi, and welcome to another Tech Bite!

When including forms in applications, developers often wonder if they need to re-validate data on the server side after having checked it on the client. Why go to the trouble of checking the data twice?

‍

Even using mandatory fields and validating the data on the button's "client action," what happens on the device is never safe and ready to use.

‍

That's why the OutSystems Platform automatically reminds us to validate all data on the server side before saving!

‍

We should add an "if node" in our application and validate the Price input parameter. Drag the IF node to your logic and write the condition.

‍

If the validation fails, the logic should raise an exception. Pick a Raise Exception Node and select a new User Exception for the first validation. 

‍

Later, you will be reusing this exception, so let’s rename it to something meaningful like Invalid Parameters. 

‍

Now, return to your Exception node and configure the Exception Message field with the message you will display to the user.

‍

That raised exception could be handled on the Screen's Client Action with an Exception Handler node, but it is unnecessary. Because, for all user exceptions without a handler, the Global Exception Handler of your module will do the work. 

‍

You can check it on the On Exception action of the "Common" UI Flow. This will trigger an error message to be shown to the end user.

‍

Let's simulate an issue in the data typed by a user. To be able to test the exception in runtime, we will temporarily disable the client-side validation.

‍

Now, if we try to input invalid data, such as a negative value in the Price field, an error message will be displayed! Meaning the application raised an exception because the data was not valid.

‍

Thanks for watching.

‍