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, IFormFieldVisitsInherits 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();}Constructors
Section titled “Constructors”ValidatingViewModel
Section titled “ValidatingViewModel”ValidatingViewModel()Protected: for classes that derive from it
Creates ValidatingViewModel.Validation; register the rules in the constructor of the derived class.
Properties
Section titled “Properties”FormError
Section titled “FormError”string? FormError { get; }Why the form cannot be submitted (the first field error), for the submit button’s tooltip.
HasErrors
Section titled “HasErrors”bool HasErrors { get; }Any error is visible below its field.
IsFormValid
Section titled “IsFormValid”bool IsFormValid { get; }No field error, shown or not: submit buttons are enabled.
Validation
Section titled “Validation”FormValidator Validation { get; }Protected: for classes that derive from it
The rules and the edited / submitted state of this form.
Methods
Section titled “Methods”ErrorOf
Section titled “ErrorOf”string? ErrorOf(string propertyName)The visible error of a property (tests, code-behind free views).
FieldLeft
Section titled “FieldLeft”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).
GetErrors
Section titled “GetErrors”IEnumerable GetErrors(string? propertyName)The shown errors of a property (INotifyDataErrorInfo).
OnValidationChanged
Section titled “OnValidationChanged”virtual void OnValidationChanged()Protected: for classes that derive from it
Called after any error changed (refresh command CanExecute here).
Events
Section titled “Events”ErrorsChanged
Section titled “ErrorsChanged”event EventHandler<DataErrorsChangedEventArgs>? ErrorsChangedThe shown errors of a property changed (INotifyDataErrorInfo).