Setting Up Data Validation
Introduction
This document explains how you can set up data validation with Mendix. Make sure you have set up a basic data structure, otherwise there is no data to validate. For more information on how to set up a basic data structure, see Creating a Basic Data Layer.
Data Validation on Entity Level
This section explains how you can add validation rules to the domain model of your module. Validation rules are always triggered when changes to an object are committed.
To add validation rules on entity level, follow the steps below:
-
Open your domain model in Studio Pro.
-
Double-click a persistable entity to open its properties.
-
Go to the Validation rules tab.
-
Click New to start configuring a new validation rule for this entity.
-
Select an Attribute whose value should be validated.
-
Enter an Error message that is shown to the end-user if the validation fails.
-
Select a Type for the validation rule.
-
Click OK to save this validation rule.
An example of an already-configured validation rule is shown below:
For more information on adding validation rules on entity level, see Validation Rules.
Required Validation on Inputs, Reference Selectors, and Drop-downs
The page editor of Studio Pro allows you to configure mandatory inputs and add error messages shown to end-users if the inputs are empty. To do so, you first need to have a detail page. For more information on how to create a detail page, see How to Create Your First Two Overview and Detail Pages.
To set required validation on input elements through the page editor, follow the steps below:
- Open a detail page and double-click an input widget to open its properties.
- Go to the Validation section and select a Type of validation.
- Enter a message for Placeholder text if applicable.
- A custom error message can also be added to Message in the Validation section. This error message is shown to the end-user if the validation fails.
An example of checking the input for the Name attribute of a Customer entity is shown below:
For more information on input widget validation, see the Validation section in Properties Common in the Page Editor.
Advanced Data Validation with the Before Commit Event
Validation rules are great for simple validations, but Mendix also offers ways to handle more complex validations. The domain model allows you to define event handlers on entity level. The Before Commit and After Commit events are triggered when an object is committed to the database. The After Commit is most commonly used to calculate values of denormalized data. With the Before Commit event, you can run a microflow that must return a Boolean value. If the microflow returns false
, the entire commit is aborted, otherwise the object is stored in the database. This mechanism is great for data validation.
This section explains how you can validate data using the Before Commit event. To do so, follow these steps:
-
Open your domain model in Studio Pro.
-
Double-click an entity to open its properties.
-
Go to the Event handlers tab.
-
Click New to start configuring a new event handler for this entity.
-
Select Before as Moment and Commit as Event. This forces the event to trigger every time an object of this entity is committed.
-
Make sure to select Yes for Pass event object, because the object holds the data that you want to validate.
-
Click Select to connect a microflow to this event.
-
Click New in the Select Microflow dialog box to create a new microflow.
-
Click OK to save the event handler and open the created microflow. It should look like this:
As long as this microflow returns a Boolean value, you are free to add any logic to determine whether the data is valid or not. If the microflow returns false
, the commit is cancelled. Returning true
commits the object.
For more information on working with microflows, see Microflows.
Advanced Validation with a Custom Save Button
Validating user input can also be achieved by overriding the default Save button on a detail page. For more information on how to create a detail page, see How to Create Your First Two Overview and Detail Pages.
There are two ways to configure a custom Save button for data validation.
You can also configure a custom Save button manually. To do so, follow these steps:
-
Right-click the Save button and select Delete to remove it from the page.
-
Right-click the drop-zone below the Cancel button and select Add widget > Buttons > Call microflow.
-
In the Select Microflow dialog box, click New to create a new microflow. It should look like this:
-
Create a Commit activity to store the object in the database.
-
Create a Close page activity to close the detail page.
You now created a microflow that mimics the default Save button behavior. It should look like this:
You can now extend the microflow to validate user input.
-
After the start event, insert a decision.
-
Double-click the decision to open its properties.
-
Type in an expression that returns
true
orfalse
. For example: $Customer/Name != ‘John’. As you can see, you can use the Customer input parameter in the expression to validate its values. -
Save the properties by clicking OK.
-
Right-click the flow between the decision and the commit activity and select true as the condition value. In this case, if the customer’s name is not ‘John’, the object is stored in the database and the page is closed.
-
Draw an additional flow from the decision and add another end event.
-
Right-click the flow between the decision and the end event and select false as the condition value.
-
Insert a Validation feedback activity between the decision and the new end event. In the activity’s properties dialog box, configure the activity in the following way:
- Select your input parameter as Variable (in the example, the Customer entity is the input parameter).
- Select an attribute of the Customer entity as Member (for example, Name).
- Enter an error message in the Template field. You can use indexes to dynamically insert parameters in the template.
Your microflow should now look like this:
Validating Multiple Attributes
If you want to validate multiple attributes, it is best to do this in a sub-microflow:
- Create a Boolean variable set to
true
. Be sure to mark this variable as the Return value. - Create your validations as described in the above section.
- Configure the sub-microflow to change the Boolean variable to
false
after the validation feedback or error message. Connect this flow back to the main sequence flow using a merge, and continue with the next validation. - At the end of the sub-microflow, the variable should be
true
if it successfully passed all validations, andfalse
when one or more validations have failed. - Add a decision that checks the return value and only allows the microflow to continue to the Commit event if all validations have passed. In this way, you can keep the logic while performing all necessary validations at once.