Implement Property Value Validation in the Application Model
- 2 minutes to read
This lesson explains how to check whether a property value meets a predefined rule.
In this lesson, you will implement the following scenario: a user cannot mark a task as completed until the task is started (DemoTask.Status set to NotStarted
).
Note
Before you proceed, take a moment to review the following lesson.
Step-by-Step Instructions
Invoke the Model Editor for the MySolution.Module project. Navigate to the Validation | Rules node. Add a new validation rule: right-click the Rules node and select Add… | RuleCriteria.
Set the following properties for the newly added node:
- TargetType to “MySolution.Module.BusinessObjects.DemoTask”
- Criteria to “Status != ‘NotStarted’”
- ID to “TaskIsNotStarted”
- TargetContextIDs to “MarkCompleted”
- CustomMessageTemplate to “Cannot set the task as completed because it has not started”
In the ActionDesign | Actions | Task.MarkCompleted node, set the Mark Completed Action’s ValidationContexts property to “MarkCompleted”.
Run the application. Assign the “Not Started” value to the Status property of one of the existing DemoTask objects. Click the MarkCompleted button. The following Validation Error dialog is displayed.
Detailed Explanation
Validation Module
To use XAF Validation Module functionality, install the DevExpress.ExpressApp.Validation.Blazor
NuGet package. The project wizard adds this package to all new applications.
When the Validation Module is added to the MySolution.Module project, the Validation node is available in the Application Model. This node defines the validation Contexts and Rules used in your application. You can use the Model Editor to add Rules and specify Contexts.
You can also add a rule to a class or property in code, when the class sources are inaccessible: Implement Property Value Validation in Code (XPO).
The following topic lists all available rule types and their descriptions: Validation Rules.
Validation Criteria
The Criteria property value must respect the Criteria Language Syntax. To set the criteria, you can click the ellipsis button () to the right of the Criteria value and invoke the Filter Builder dialog. In this dialog, you can visually design a criteria expression.
Validation Context
The TargetContextIDs property is set to “MarkCompleted”. This means that the rule is checked when a user executes an Action whose ValidationContexts property is set to “MarkCompleted”.
Tip
You can use the Save or Delete contexts, which are available by default. Rules with these contexts are validated when an object is saved or deleted, respectively (see Validation Rules).