Implement Property Value Validation in Code (XPO)
- 4 minutes to read
Tip
For .NET 6 applications, see: Implement Dependent Reference Properties (XPO, .NET 6).
This lesson explains how to set rules for business classes and their properties. These rules are validated when an end-user executes a specified operation. This lesson will guide you through implementation of a rule that requires that the Position.Title property must not be empty. This rule will be checked when saving a Position object. You will also be able to see the user interface elements that report a broken rule.
Note
Before proceeding, take a moment to review the following lessons:
The validation functionality is provided by the Validation Module. Add this module to your MySolution.Module project. For this purpose, find the Module.cs (Module.vb) file in the MySolution.Module project displayed in the Solution Explorer. Double-click this file to invoke the Module Designer. In the Toolbox, navigate to the DX.22.1: XAF Modules section. Drag the ValidationModule item from this section to the Designer’s Required Modules panel. Rebuild your solution.
In a WinForms application, add the ValidationWindowsFormsModule. This module creates validation error messages that are more informative and user friendly than the default exception messages. Additionally, this module provides in-place validation support (see IModelValidationContext.AllowInplaceValidation). To add this module, find the WinApplication.cs (WinApplication.vb) file in the MySolution.Win project displayed in the Solution Explorer, double-click this file to invoke the Application Designer and drag the ValidationWindowsFormsModule from the Toolbox to the Required Modules panel.
In an ASP.NET Web Forms application, you can also add the ValidationAspNetModule. This module provides in-place validation support (see IModelValidationContext.AllowInplaceValidation). To add this module, find the WebApplication.cs (WebApplication.vb) file in the MySolution.Web project displayed in the Solution Explorer, double-click this file to invoke the Application Designer and drag the ValidationAspNetModule from the Toolbox to the Required Modules panel.
Apply the RuleRequiredFieldAttribute attribute to the Position class’ Title property. As a parameter, specify the context for checking the rule (e.g., DefaultContexts.Save). The following code demonstrates this attribute.
using DevExpress.Persistent.Validation; //... [DefaultClassOptions] [System.ComponentModel.DefaultProperty(nameof(Title))] public class Position : BaseObject { //... private string title; [RuleRequiredField(DefaultContexts.Save)] public string Title { get { return title; } set { SetPropertyValue(nameof(Title), ref title, value); } } }
The RuleRequiredField attribute defines a validation rule that ensures that the Position.Title property has a value when the Position object is saved.
Run the WinForms or ASP.NET Web Forms application. Click the New () button to create a new Position. Leave the Title property empty and click the Save button. The following error message will be displayed, depending on the application type.
WinForms Application
ASP.NET Web Forms Application
This warning message will also be invoked if you click the Save and Close button, or perform another action that saves the object to the database.
Note
You can use the Validate toolbar button to check to see if there are broken rules without saving the current object.
- In the WinForms application, close the window with the warning message, set a value for the Title property and click the Save button. In the ASP.NET Web Forms application, set a value for the Title property and click the Save button. The object will be saved successfully.
Note
The Validation System provides a number of Rules and Contexts. For details, refer to the Validation Rules topic. Information on Rules applied in code is loaded into the Application Model (see the Implement Property Value Validation in the Application Model topic). This allows a business application administrator to add and edit Rules and Contexts via the Model Editor.
You can see the code demonstrated here in the MySolution.Module | Business Objects | Contact.cs (Contact.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 22.1\Components\XAF\MainDemo by default. The ASP.NET Web Forms version is available online at https://demos.devexpress.com/XAF/MainDemo.
Next Lesson: Add a Simple Action