FormValidator
Class in Oadm.Sdk.Client.Validation · Oadm.Sdk.Client.dll
Field validation of a form view model for INotifyDataErrorInfo: every rule reports on the property it belongs to, so Avalonia shows the message directly below that input (DataValidationErrors, inside ui:FormField). Rules always run; an error becomes visible once the user edited the field (FormValidator.Touch) or tried to submit (FormValidator.ShowAll), never on an untouched empty form. Server-side errors (FormValidator.SetServerError) are visible at once and cleared when the field is edited. Submit buttons use FormValidator.IsValid / FormValidator.FirstError (all errors, visible or not) for IsEnabled and the tooltip that says why. Use ValidatingViewModel as base class, or own one of these and forward INotifyDataErrorInfo to it.
public sealed class FormValidatorGuide: UI kit
Constructors
Section titled “Constructors”FormValidator
Section titled “FormValidator”FormValidator(Action<string> errorsChanged, Action? stateChanged = null)Parameters
errorsChanged: RaisesINotifyDataErrorInfo.ErrorsChangedfor a property.stateChanged: Called after a validation pass changed any error (refresh commands, tooltips).
Properties
Section titled “Properties”Fields
Section titled “Fields”IReadOnlyList<string> Fields { get; }The fields with rules, in registration order.
FirstError
Section titled “FirstError”string? FirstError { get; }The first error in field order, shown or not (tooltip of the disabled submit button); null when valid.
HasErrors
Section titled “HasErrors”bool HasErrors { get; }Any error is visible (INotifyDataErrorInfo.HasErrors).
IsValid
Section titled “IsValid”bool IsValid { get; }No error at all, shown or not yet shown: the form can be submitted.
this[]
Section titled “this[]”string? this[string property] { get; }The visible error of a property (null when none or not shown yet).
Methods
Section titled “Methods”ErrorOf
Section titled “ErrorOf”string? ErrorOf(string property)The current error of a property, shown or not.
FirstErrorOf
Section titled “FirstErrorOf”string? FirstErrorOf(IEnumerable<string>? properties)The first error among the given properties (all when null), in field order.
GetErrors
Section titled “GetErrors”IEnumerable GetErrors(string? property)The shown errors of property (for INotifyDataErrorInfo).
IsField
Section titled “IsField”bool IsField(string? property)True when the property has a rule.
IsValidFor
Section titled “IsValidFor”bool IsValidFor(params string[] properties)True when none of the given properties has an error (shown or not).
void Reset(params string[] properties)Back to an untouched form (after loading or prefilling values, after a successful submit, when an editor reopens): errors are hidden again until the next edit or submit, server errors are cleared. Only properties when given.
FormValidator Rule(string property, Func<string?> rule)Adds a rule for one property: returns the message, or null when the value is fine.
FormValidator Rules(IEnumerable<string> properties, Func<IReadOnlyDictionary<string, string?>> rules)Adds a rule set that checks several properties at once (a shared validator returning property -> message). Of several rules on the same property the first registered one with a message wins.
SetServerError
Section titled “SetServerError”void SetServerError(string property, string? error)An error the server (or the device) reported for this field, e.g. “The login failed.” on the password. Shown at once, below the field; cleared when the user edits the field. Null clears it.
ShowAll
Section titled “ShowAll”void ShowAll(params string[] properties)The user tried to submit: every error becomes visible (or only those of properties).
void Touch(string? property)The user edited the property: its error becomes visible, its server error is cleared; validates.
Validate
Section titled “Validate”void Validate()Runs every rule again; raises the change for every property whose visible error changed.