Back to Devexpress

Trigger Validation Programmatically and Customize Default Rule Behavior

expressappframework-113010-validation-trigger-validation-programmatically-customize-default-rule-behavior.md

latest7.0 KB
Original Source

Trigger Validation Programmatically and Customize Default Rule Behavior

  • Feb 21, 2026
  • 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.

ValidateTargetChecks whether the specified object satisfies all rules associated with specified contexts.ValidateAllTargetsChecks whether specified objects satisfy all rules associated with specified contexts.ValidateChecks whether the specified object satisfies all rules associated with specified contexts. If validation fails, the method raises an exception.ValidateAllChecks 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.

csharp
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 XAF 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:

csharp
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:

csharp
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) {
        //...
    }
}

The following events are implemented in the RuleSet class:

CustomIsEmptyValueOccurs when the RuleSet.IsEmptyValue method is executed to determine whether or not the property value is considered empty.CustomIsEmptyValueTypeCustomNeedToValidateRuleOccurs when the validation system determines whether or not a rule should be validated.CustomValidateRuleOccurs when a rule is being validated.RuleValidatedOccurs after the validation of a rule is complete.ValidationCompletedOccurs after validation of an entire RuleSet is complete.

See Also

Implement Custom Rules

Validation Rules

How to Highlight Invalid Properties Immediately in an Invoked View

ModificationsController.ModificationsHandlingMode