Back to Devexpress

IRuleSet Interface

expressappframework-devexpress-dot-persistent-dot-validation-6705abdc.md

latest5.3 KB
Original Source

IRuleSet Interface

Specifies a set of Validation Rules that can be checked against an object.

Namespace : DevExpress.Persistent.Validation

Assembly : DevExpress.Persistent.Base.v25.2.dll

NuGet Package : DevExpress.Persistent.Base

Declaration

csharp
public interface IRuleSet
vb
Public Interface IRuleSet

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 an IRuleSet:

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

How to Subscribe to Validation Events

You can use the ValidationOptions class to handle validation events. 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:

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

See Also

IRuleSet Members

IValidator

DevExpress.Persistent.Validation Namespace