Implement Property Value Validation
- 3 minutes to read
This lesson explains how to set validation rules for entity classes and their properties. XAF applies these rules when a user executes a specific operation (for example, saves an edited object).
The Validation System offers a number of predefined rule types and contexts. A rule specifies a data validity condition. A context defines the moment when the application checks the rule.
To use XAF Validation functionality, you need the DevExpress.ExpressApp.Validation.Blazor
and DevExpress.ExpressApp.Validation.Win
NuGet packages. When you created the solution and selected the Validation module in the project wizard, the wizard added these packages to platform-specific projects.
Tip
To add this functionality to an existing application, you need to install the NuGet packages and register the modules. For additional information, refer to the following section: Validation System Elements.
The instructions below explain how to do the following:
- Use the Model Editor to prevent a user from marking a task as completed before the task starts (
DemoTask.Status
isNotStarted
). - Create a rule in code that requires that the
Position.Title
property must not be empty.
Implement Property Value Validation in Model Editor
Open the Model.DesignedDiffs.xafml file in the Model Editor. Navigate to the Validation | Rules node. Right-click the Rules node and select Add… | RuleCriteria.
Set values of the following properties for the node:
- TargetType to
MySolution.Module.BusinessObjects.DemoTask
- Criteria to
Status != 'NotStarted'
- ID to
TaskStarted
- TargetContextIDs to
MarkCompleted
- CustomMessageTemplate to
Cannot set the task as completed because it has not started.
The Criteria property value must respect the Criteria Language Syntax. To set the criteria, 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.
- TargetType to
Navigate to the ActionDesign | Actions | DemoTask.MarkCompleted node and set the ValidationContexts property to
MarkCompleted
.Change the caption of the MarkCompleted Action to
Mark Completed
.Run the application and go to one of the unfinished tasks. Click the Mark Completed button. The following Validation Error dialog appears:
- ASP.NET Core Blazor
- Windows Forms
Implement Property Value Validation in Code
Apply the
RuleRequiredField
attribute to theTitle
property in thePosition
class. Specify the context that triggers the rule (for example,DefaultContexts.Save
) as the attribute’s parameter:// ... using DevExpress.Persistent.Validation; //... { [DefaultClassOptions] [DefaultProperty(nameof(Title))] public class Position : BaseObject { /*Define a validation rule that ensures that the `Position.Title` property has a value when you save the `Position` object.*/ [RuleRequiredField(DefaultContexts.Save)] public virtual string Title { get; set; } //... } }
Run the application and go to the Position List View.
Click the New button to create a new Position object. Leave the
Title
property empty and click Save. The following Validation Error dialog appears:- ASP.NET Core Blazor
- Windows Forms