IRuleSet Interface
Specifies a set of Validation Rules that can be checked against an object.
Namespace: DevExpress.Persistent.Validation
Assembly: DevExpress.Persistent.Base.v24.2.dll
NuGet Package: DevExpress.Persistent.Base
#Declaration
#Related API Members
The following members return IRuleSet objects:
#Remarks
Validation is based on rules declared in business class code and in the Validation node of the Application Model. Internally, these rules are automatically collected in the RegisteredRules property of an IRuleSet
object. The rules are checked automatically when objects are saved or deleted and when an Action with the appropriate validation context is executed.
If you need to modify the default behavior, you can use one of the following techniques to access an object that implements the IRuleSet
interface (both examples demonstrate how to trigger validation against a specific object and context, and then handle the result):
Use Dependency Injection to access the IValidator service. Use the injected service’s
RuleSet
property to access anIRuleSet
:File: MySolution.Module.Controllers.MyController.cs
using DevExpress.Persistent.Validation; using Microsoft.Extensions.DependencyInjection; // ... namespace MySolution.Module.Controllers; public partial class MyController : ViewController { IServiceProvider serviceProvider; public MyController() { } [ActivatorUtilitiesConstructor] public MyController(IServiceProvider serviceProvider) : this() { InitializeComponent(); this.serviceProvider = serviceProvider; } protected override void OnActivated() { base.OnActivated(); RuleSetValidationResult result = serviceProvider.GetRequiredService<IValidator>() .RuleSet.ValidateTarget(View.ObjectSpace, View.CurrentObject, "MyContext"); if (result.ValidationOutcome > ValidationOutcome.Information) ((Contact)View.CurrentObject).Notes += "[Validation Error] " + result.Results[0].ErrorMessage; } }
Call the static Validator.GetService method. This method takes an
IServiceProvider
instance as a parameter. In a context where XafApplication is available (for example, in a View Controller), you can use this method as follows:File: MySolution.Module.Controllers.MyController.cs
using DevExpress.Persistent.Validation; // ... namespace MySolution.Module.Controllers; public partial class MyController : ViewController { // ... protected override void OnActivated() { base.OnActivated(); RuleSetValidationResult result = Validator.GetService(Application.ServiceProvider).ValidateTarget( View.ObjectSpace, View.CurrentObject, "MyContext" ); if (result.ValidationOutcome > ValidationOutcome.Information) ((Contact)View.CurrentObject).Notes += "[Validation Error] " + result.Results[0].ErrorMessage; } }
#How to Subscribe to Validation Events in a .NET Application
You can use the ValidationOptions
class to handle validation events in .NET applications. The ValidationOptions.Events
property
exposes the following delegate properties:
OnCustomIsEmptyValue
OnCustomIsEmptyValueType
OnCustomNeedToValidateRule
OnCustomValidateRule
OnRuleValidated
OnValidationCompleted
You can assign event handlers to their properties in two ways: use the XAF Application Builder or get a ValidationOptions
instance as IOptionsSnapshot.
#Use the XAF Application Builder
The following code sample subscribes to the OnCustomNeedToValidateRule
event in the XAF Application Builder:
services.AddXaf(Configuration, builder => {
//...
builder.Modules
//...
.AddValidation(options => {
options.Events.OnCustomNeedToValidateRule += (context) => {
//...
};
});
//...
};
#Get a ValidationOptions instance as IOptionsSnapshot
The following code sample uses dependency injection and IOptionsSnapshot to subscribe to the OnCustomNeedToValidateRule
event in an XAF controller:
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Validation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
public class CustomValidationCheckController : WindowController {
IOptionsSnapshot<ValidationOptions> options;
[ActivatorUtilitiesConstructor]
public CustomValidationCheckController(IServiceProvider serviceProvider) : this() {
this.TargetWindowType = WindowType.Main;
options = serviceProvider.GetRequiredService<IOptionsSnapshot<ValidationOptions>>();
}
public CustomValidationCheckController() { }
protected override void OnActivated() {
base.OnActivated();
options.Value.Events.OnCustomNeedToValidateRule += CustomNeedToValidateRule;
}
protected override void OnDeactivated() {
options.Value.Events.OnCustomNeedToValidateRule -= CustomNeedToValidateRule;
base.OnDeactivated();
}
private void CustomNeedToValidateRule(CustomNeedToValidateRuleContext context) {
//...
}
}