Trigger Validation Programmatically and Customize Default Rule Behavior
- 3 minutes to read
The XAF validation module allows you to configure validation parameters, trigger validation in code, and change the validation logic at runtime.
#Trigger Validation Programmatically
The IRuleSet interface declares the following validation methods you can call in a custom place in your code to trigger validation.
- ValidateTarget
- Checks whether the specified object satisfies all rules associated with specified contexts.
- ValidateAllTargets
- Checks whether specified objects satisfy all rules associated with specified contexts.
- Validate
- Checks whether the specified object satisfies all rules associated with specified contexts. If validation fails, the method raises an exception.
- ValidateAll
- Checks whether specified objects satisfy all rules associated with specified contexts. If validation fails, the method raises an exception.
These methods should be used from Controllers and Object Space events, and not within the persistent class scope that takes the Object Space instance as a parameter.
You can use this methods to validate rules in predefined or custom validation contexts. Call the GetService(IServiceProvider) method to access the IRuleSet object.
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using YourApplicationName.Module.BusinessObjects;
namespace YourApplicationName.Blazor.Server.Controllers;
public class CustomBlazorController : ViewController {
public CustomBlazorController() {
var myBlazorAction = new SimpleAction(this, "MyBlazorAction", PredefinedCategory.Edit);
myBlazorAction.Execute += (s, e) => {
var os = Application.CreateObjectSpace(typeof(DemoTask));
var tsk = os.FirstOrDefault<DemoTask>(t => t.Subject == "Task1");
var employee = os.CreateObject<Employee>();
employee.FirstName = "James";
tsk.AssignedTo = employee;
IRuleSet ruleSet = Validator.GetService(Application.ServiceProvider);
ruleSet.Validate(os, employee, DefaultContexts.Save);
os.CommitChanges();
};
}
}
#Customize Default Rule Behavior
The XAF validation module implements a set of validation-related events that allow you to change the predefined validation logic at runtime.
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 an 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) {
//...
}
}