Validation (Prevent Data Errors)
- 4 minutes to read
Different tasks in business applications often require information that is to be edited to meet special requirements. Sometimes end-user actions may not meet these requirements. Consider a simple example where an end-user fills out a form with personal information. It is usually necessary to fill out all the required fields, but the end-user may be unaware of this. Special notifications should be provided to ensure that all required data is entered. Another example relates to the internal business logic of an application. For example, a rule may require that the total sum of an order not exceed a certain value.
Validation Rules like these should be controlled by the application, so that end-users are unable to enter invalid data. Sometimes, you may need to change the rules according to your requirements. If you implemented your rules with custom code, you will need to change your code and re-build the application. At the same time, this approach does not let administrators change Validation Rules. To avoid these problems, XAF provides a special Validation module. This module allows for the easy and effective control of user input. This topic details the basic concepts of this tool. To learn how to use it to provide data validation in your application, refer to the Declare Validation Rules topic.
Contextual Validation Concept
XAF uses the following validation concept. Restrictions can be applied to business classes and their properties. For example, filling in a particular property is required when saving an object, or the value of a certain property must be within 10 and 20 (also checked when saving an object). For these requirements, IsRequiredField and Range are the Rules, and that moment the Rules (e.g., object saving) are checked is called the Context. Thus, Validation Rules and Validation Contexts are the main concepts of the Validation system.
Validation in UI
Validation is organized in the UI with the PersistenceValidationController that manages the adjustment of rules. Thus, it is possible to specify whether objects with a particular property value need to be validated, modify a collection of aggregated objects or a collection of all objects to be validated in particular context. For additional technical information, refer to this controller description.
Validation System Elements
The Validation Module comprises three assemblies.
DevExpress.ExpressApp.Validation.v24.1.dll
Contains base validation-specific classes and services. This assembly represents a module, since it contains the ValidationModule class - a descendant of the ModuleBase class. This module also uses entities from the DevExpress.Persistent.Base.v24.1.dll assembly, which is referenced by default: base classes and interfaces, common Validation Rules implementation and static validation services that are used to collect Validation Rules and check objects.
DevExpress.ExpressApp.Validation.Win.v24.1.dll
Implements WinForms-specific validation functionality. This assembly is a module, since it contains the ValidationWindowsFormsModule class - a descendant of the ModuleBase class. This module creates validation error messages (invoked after rules are broken) that are more informative and user friendly than default exception messages. Additionally, this module provides in-place validation support (see IModelValidationContext.AllowInplaceValidation).
DevExpress.ExpressApp.Validation.Blazor.v24.1.dll
Implements ASP.NET Core Blazor-specific validation functionality. This assembly is a module, since it contains the DevExpress.ExpressApp.Validation.Blazor.ValidationBlazorModule class - a descendant of the ModuleBase class. This module creates validation error messages (invoked after rules are broken) that are more informative and user friendly than default exception messages.
DevExpress.ExpressApp.Validation.Web.v24.1.dll
Implements ASP.NET Web Forms-specific validation functionality. This assembly is a module, since it contains the ValidationAspNetModule class - a descendant of the ModuleBase class. This module provides in-place validation support (see IModelValidationContext.AllowInplaceValidation).
Add Validation Module to an XAF Application
You can add modules to your application when you use the Solution Wizard to create a new XAF solution. Select modules in the Choose Additional Modules step.
- To add an extra module in code, add it to the XafApplication.Modules or ModuleBase.RequiredModuleTypes list (adding a reference to the module assembly is insufficient).
In .NET applications, you can call the AddValidation(IModuleBuilder<IBlazorApplicationBuilder>, Action<ValidationModuleOptions>) / AddValidation(IModuleBuilder<IWinApplicationBuilder>, Action<ValidationModuleOptions>) method in your ASP.NET Core Blazor / WinForms application builder.
- To add this module to your .NET Framework applications, you can also invoke the Application Designer and drag the Module from the Toolbox to the Modules panel.
You can see many examples of the Validation Module in use in the Validation section of the FeatureCenter demo, which is supplied with XAF. This demo is located in the %PUBLIC%\Documents\DevExpress Demos 24.1\Components\XAF\FeatureCenter.NETFramework.XPO folder, by default.