Table of Contents

Form Component Custom Business Rule

Inherit from the class FormComponentCustomBusinessRule and override the desired business rule methods in order to implement a concrete rule for a Form Component.

Validation methods

  • ApplyBusinessRulesOnSaveAsync: Use this method to validate and/or modify form fields before creation or editing.
  • ApplyBusinessRulesOnDeleteAsync: Validate the delete operation of the main item of the page, or many items when the request comes from bulk edit page.

Dependency Injection

MIB supports dependency injection in concrete validators. Click here for more information.

Example

In this hypothetical validation, we override the validation methods ApplyBusinessRulesOnSaveAsync and ApplyBusinessRulesOnDeleteAsync. We use the inherited method AddNotification to add notifications to describe issues with the persistence data.

In the hypothetical case of changes to field values, we should override the method ApplyBusinessRulesOnSaveAsync and use the Dictionary<string, object> fields to access and modify the persistence values of the fields.

Key points

  • Notifications will be aggregated and displayed to the user.
  • Dictionary keys can be used in the messages.
public class FormBusinessRule : FormComponentCustomBusinessRule
{
    public override Task ApplyBusinessRulesOnSaveAsync(Dictionary<string, object> fields, FormComponentContext context, CancellationToken cancellationToken)
    {
        if (fields.ContainsKey("NAME"))
            fields["NAME"] = "Modified by business rule";

        if (fields.ContainsKey("OWNER"))
            fields["OWNER"] = 2;

        return Task.CompletedTask;
    }

    public override Task ApplyBusinessRulesOnDeleteAsync(IReadOnlyList<int> mibObjectIds, FormComponentContext context, CancellationToken cancellationToken)
    {
        AddNotification(new("My rules do not allow deletion"));

        return Task.CompletedTask;
    }
}