Validation
Rules, timing, the fluent builder and cross block rules.
In this documentation
- Beep.Forms
- Architecture
- Authoring in the Object Navigator
- WinForms runtime
- WPF runtime
- End-to-end example
- Remote UI limitations
- Overview
- Context menus
- Blocks and items
- Triggers
- Lists of values
- Validation
- Master detail
- Navigation and queries
- Alerts, editors and timers
- Calling other forms
- Form menus
- Hosting in WinForms step by step
- Hosting in WPF step by step
- Saving and loading forms
- The IDE toolbar and dialogs
- Troubleshooting
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)andmanager.ValidateForm()validate on demand.ValidateFormfires theWhenValidateFormtrigger.
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.