This how-to explains how you can set up data validation with Mendix. Before you can start with this how-to you need to setup a basic data structure, otherwise there won’t be any data to validate. If you don’t know how to setup a basic data structure, you can take a look at this how-to.
1 Data Validation on Entity Level
This section will explain 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. Please note that validation rules can only be applied on entities that are persistable.
- Open a domain model in the Desktop Modeler.
- Double click a persistable entity to open its properties.
Open the Validation Rules tab page.
Click New to start configuring a new validation rule for this entity.
Select the attribute of which the value should be validated.
Enter an error message that is shown to the user if the validation fails.
Select the rule type.
Click OK to save this validation rule.
2 Required Validation on Inputs, Reference Selectors and Drop-downs
The page builder of the Desktop Modeler allows you to configure which inputs are mandatory and what message is shown to the user if the input is empty. If you don’t have a detail page yet, take a look at this how-to.
- Open a detail page and double click an input to open its properties.
- Enter a message for Placeholder text. This message is shown below the input if a user clicks the save button without filling in a value.
Please note that a required validation rule on entity level overrules this property.
3 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 always triggered when an object is committed to the database. The ‘After Commit’ is most commonly used to calculate values of de-normalized data. With the ‘Before Commit’ event you can execute 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 will explain how you can validate data by use of the ‘Before Commit’ event.
- Open a domain model in the Desktop Modeler.
- Double click an entity to open its properties.
- Open the Event handlers tab page.
- 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 that the event object is passed, because this object holds the data that you want to validate.
Click Select to connect a Microflow to this event.
Click New in the Microflow selector to create a new one.
Click OK to save the event handler and open the created Microflow. It should look similar to this:
As long as this Microflow returns a Boolean value, you are free to add any logic to determine if the data is valid or not. If the Microflow returns ‘false’, the commit is cancelled. Returning ‘true’ will commit the object. If you don’t know how to work with microflows, take a look at how-to.
4 Advanced Validation with a Custom ‘Save’ Button
Validating user input can also be achieved by overriding the default save button on a detail page. Start by creating an overview and detail page. If you don’t know how to create overview and detail pages, take a look at this how-to. Your detail page should look similar to this:
- 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 > Button widgets > 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 have 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 an Exclusive split.
Double click the split to open the properties editor and type in an expression that returns true or false. 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 line between the split and commit activity and select true as condition value. In this case if the customer’s name not is ‘John’, the object is stored in the database and the page is closed.
Add an End event to the Exclusive Split.
Draw an additional line from the split to the new end event.
Right click the line between the split and end event and select false as condition value.
Insert a Validation feedback activity between the split and end event. In the activity’s properties editor, enter these details:
Your microflow should now look similar to this:
Select the input parameter as variable.
Select for example Name as member.
Enter an error message in the Template field. You can use indexes to dynamically insert parameters in the template.