Rules

manager.Validation.RegisterRule(new ValidationRule
{
    RuleName = "Qty_Range", BlockName = "Ord", ItemName = "Qty",
    ValidationType = ValidationType.Range,
    ErrorMessage = "Quantity must be between 0 and 100.",
    IsEnabled = true, MinValue = 0, MaxValue = 100,
});

ValidationType values: Required, Range, Pattern, MaxLength, MinLength, Lookup, Unique, Custom, Email, Url, Date, Numeric, GreaterThan, LessThan, EqualTo, CrossField, Database.

ValidationTiming values: OnBlur, OnChange, OnRecordChange, OnCommit, Manual. The default is OnBlur. ValidationSeverity values: Error (the default), Warning, Info, Critical.

Other ValidationRule members: Pattern, LookupSource, CompareFieldName, CustomValidator, Condition, ConditionFunction, ExecutionOrder, StopOnFailure (true) and Description. The custom validator is a function taking the new and old values and a dictionary, and returning a task of a pair of a valid flag and an error message.

An error message may contain the placeholders {FieldName}, {Value}, {Min}, {Max}, {Pattern} and {CompareField}. Factories: Required, Range, ForPattern, MaxLength, Custom.

Fluent builder

manager.Validation.ForField("Ord", "Qty")
    .Required()
    .Range(0, 100)
    .WithMessage("Quantity must be between 0 and 100.")
    .Register();

The builder also has MinLength, MaxLength, Pattern, MustBe, CannotBe, Custom, WithName, WithOrder, WithSeverity, WithTiming and StopOnFailure.

The validation manager

IValidationManager offers RegisterRule, RegisterRules, UnregisterRule, UnregisterBlockRules, UnregisterItemRules, ClearAllRules, GetRule, GetRulesForItem, GetRulesForBlock, GetAllRules, GetRulesByTiming, ValidateItem, ValidateRecord, ValidateBlock, ValidateForm with async variants, SetRuleEnabled, SetBlockValidationEnabled, and the events ValidationStarting, ValidationCompleted and ValidationFailed.

When rules run

  • When an item changes on the current record, rules with OnChange timing run and the item error is stored or cleared.
  • On Tab or Enter, the block host validates the item with OnBlur timing, then fires WhenValidateItem.
  • manager.ValidateBlock(block) and manager.ValidateForm() validate on demand. ValidateForm fires the WhenValidateForm trigger.

Cross block rules

A CrossBlockValidationRule has RuleName, BlockA, BlockB, Severity and a Validator that takes the two blocks' units of work and returns a message. A null or empty return means the rule passed. Use RegisterCrossBlockRule, UnregisterCrossBlockRule and ValidateCrossBlock.

Authoring in the IDE

The validation dialog takes an expression for some types. A range is written as "min..max" and sets MinValue and MaxValue. Pattern sets Pattern. MaxLength sets MaxValue. MinLength sets MinValue. Lookup sets LookupSource. GreaterThan, LessThan and EqualTo set CompareFieldName. Custom and CrossField set Condition. The other types take no expression.