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 How to Create a Basic Data Layer.
2 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 Studio Pro.
- 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.
3 Required Validation on Inputs, Reference Selectors and Drop-downs
The page builder of Studio Pro 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.
4 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 Studio Pro.
- 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.
5 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 How to Create Your First Two Overview & Detail Pages. 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 a decision.
Double click the decision 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 decision 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 decision.
Draw an additional line from the decision to the new end event.
Right click the line between the decision and end event and select false as condition value.
Insert a Validation feedback activity between the decision 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.
6 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 above.
- Configure the sub-microflow to change the Boolean variable to
falseafter 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
trueif it succesfully passed all validations, and
falsewhen 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 are passed. This way, you can keep the logic while performing all necessary validations at once.