Skip to content

ValidatingViewModel

Class in Oadm.Sdk.Client.Validation · Oadm.Sdk.Client.dll

Base class of every form view model (host and plugins): an ObservableObject that reports field errors through INotifyDataErrorInfo from the rules registered on ValidatingViewModel.Validation. Setting a field property (one with a rule) marks it edited and shows its error; any property change re-runs the rules (rules may depend on other properties, e.g. a mode choice). Bind the submit button to ValidatingViewModel.IsFormValid (IsEnabled) and ValidatingViewModel.FormError (ToolTip.Tip, with ToolTip.ShowOnDisabled), or use the command’s CanExecute with FormValidator.IsValid and override ValidatingViewModel.OnValidationChanged to refresh it.

public abstract class ValidatingViewModel : ObservableObject, INotifyDataErrorInfo, IFormFieldVisits

Inherits ObservableObject
Implements INotifyDataErrorInfo, IFormFieldVisits

Guide: UI kit

Example

public MyViewModel()
{
Validation.Rule(nameof(UserName), () => UserName.Trim().Length == 0 ? "Enter a user name." : null);
Validation.Validate();
}
ValidatingViewModel()

Protected: for classes that derive from it

Creates ValidatingViewModel.Validation; register the rules in the constructor of the derived class.

string? FormError { get; }

Why the form cannot be submitted (the first field error), for the submit button’s tooltip.

bool HasErrors { get; }

Any error is visible below its field.

bool IsFormValid { get; }

No field error, shown or not: submit buttons are enabled.

FormValidator Validation { get; }

Protected: for classes that derive from it

The rules and the edited / submitted state of this form.

string? ErrorOf(string propertyName)

The visible error of a property (tests, code-behind free views).

void FieldLeft(string propertyName)

The user left the input of propertyName (FormField on lost focus): its error shows now, also when the field was never edited (an empty required field).

IEnumerable GetErrors(string? propertyName)

The shown errors of a property (INotifyDataErrorInfo).

virtual void OnValidationChanged()

Protected: for classes that derive from it

Called after any error changed (refresh command CanExecute here).

event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged

The shown errors of a property changed (INotifyDataErrorInfo).