Skip to content

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 FormValidator

Guide: UI kit

FormValidator(Action<string> errorsChanged, Action? stateChanged = null)

Parameters

IReadOnlyList<string> Fields { get; }

The fields with rules, in registration order.

string? FirstError { get; }

The first error in field order, shown or not (tooltip of the disabled submit button); null when valid.

bool HasErrors { get; }

Any error is visible (INotifyDataErrorInfo.HasErrors).

bool IsValid { get; }

No error at all, shown or not yet shown: the form can be submitted.

string? this[string property] { get; }

The visible error of a property (null when none or not shown yet).

string? ErrorOf(string property)

The current error of a property, shown or not.

string? FirstErrorOf(IEnumerable<string>? properties)

The first error among the given properties (all when null), in field order.

IEnumerable GetErrors(string? property)

The shown errors of property (for INotifyDataErrorInfo).

bool IsField(string? property)

True when the property has a rule.

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.

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.

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.

void Validate()

Runs every rule again; raises the change for every property whose visible error changed.