Class ObservableValidator
- Namespace
- CommunityToolkit.Mvvm.ComponentModel
- Assembly
- CommunityToolkit.Mvvm.dll
A base class for objects implementing the INotifyDataErrorInfo interface. This class also inherits from ObservableObject, so it can be used for observable items too.
public abstract class ObservableValidator : ObservableObject, INotifyPropertyChanged, INotifyPropertyChanging, INotifyDataErrorInfo
- Inheritance
-
ObservableValidator
- Implements
- Inherited Members
Constructors
ObservableValidator()
Initializes a new instance of the ObservableValidator class. This constructor will create a new ValidationContext that will be used to validate all properties, which will reference the current instance and no additional services or validation properties and settings.
protected ObservableValidator()
ObservableValidator(IDictionary<object, object?>?)
Initializes a new instance of the ObservableValidator class. This constructor will create a new ValidationContext that will be used to validate all properties, which will reference the current instance.
protected ObservableValidator(IDictionary<object, object?>? items)
Parameters
items
IDictionary<object, object>A set of key/value pairs to make available to consumers.
ObservableValidator(ValidationContext)
Initializes a new instance of the ObservableValidator class. This constructor will store the input ValidationContext instance, and it will use it to validate all properties for the current viewmodel.
protected ObservableValidator(ValidationContext validationContext)
Parameters
validationContext
ValidationContextThe ValidationContext instance to use to validate properties.
This instance will be passed to all TryValidateObject(object, ValidationContext, ICollection<ValidationResult>) calls executed by the current viewmodel, and its MemberName property will be updated every time before the call is made to set the name of the property being validated. The property name will not be reset after that, so the value of MemberName will always indicate the name of the last property that was validated, if any.
Exceptions
- ArgumentNullException
Thrown if
validationContext
is null.
ObservableValidator(IServiceProvider?, IDictionary<object, object?>?)
Initializes a new instance of the ObservableValidator class. This constructor will create a new ValidationContext that will be used to validate all properties, which will reference the current instance.
protected ObservableValidator(IServiceProvider? serviceProvider, IDictionary<object, object?>? items)
Parameters
serviceProvider
IServiceProviderAn IServiceProvider instance to make available during validation.
items
IDictionary<object, object>A set of key/value pairs to make available to consumers.
Properties
HasErrors
public bool HasErrors { get; }
Property Value
Methods
ClearErrors(string?)
Clears the validation errors for a specified property or for the entire entity.
protected void ClearErrors(string? propertyName = null)
Parameters
propertyName
stringThe name of the property to clear validation errors for. If a null or empty name is used, all entity-level errors will be cleared.
GetErrors(string?)
public IEnumerable<ValidationResult> GetErrors(string? propertyName = null)
Parameters
propertyName
string
Returns
SetProperty<T>(T, T, Action<T>, bool, string)
Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. Similarly to the SetProperty<T>(T, T, Action<T>, string?) method, this overload should only be used when SetProperty<T>(ref T, T, string?) can't be used directly.
protected bool SetProperty<T>(T oldValue, T newValue, Action<T> callback, bool validate, string propertyName = null)
Parameters
oldValue
TThe current property value.
newValue
TThe property's value after the change occurred.
callback
Action<T>A callback to invoke to update the property value.
validate
boolIf true,
newValue
will also be validated.propertyName
string(optional) The name of the property that changed.
Returns
Type Parameters
T
The type of the property that changed.
Remarks
This method is just like SetProperty<T>(T, T, Action<T>, string?), just with the addition
of the validate
parameter. As such, following the behavior of the base method,
the PropertyChanging and PropertyChanged events
are not raised if the current and new value for the target property are the same.
Exceptions
- ArgumentNullException
Thrown if
callback
orpropertyName
are null.
SetProperty<T>(T, T, IEqualityComparer<T>, Action<T>, bool, string)
Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. See additional notes about this overload in SetProperty<T>(T, T, Action<T>, bool, string).
protected bool SetProperty<T>(T oldValue, T newValue, IEqualityComparer<T> comparer, Action<T> callback, bool validate, string propertyName = null)
Parameters
oldValue
TThe current property value.
newValue
TThe property's value after the change occurred.
comparer
IEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
callback
Action<T>A callback to invoke to update the property value.
validate
boolIf true,
newValue
will also be validated.propertyName
string(optional) The name of the property that changed.
Returns
Type Parameters
T
The type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
comparer
,callback
orpropertyName
are null.
SetProperty<T>(ref T, T, bool, string)
Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event.
protected bool SetProperty<T>(ref T field, T newValue, bool validate, string propertyName = null)
Parameters
field
TThe field storing the property's value.
newValue
TThe property's value after the change occurred.
validate
boolIf true,
newValue
will also be validated.propertyName
string(optional) The name of the property that changed.
Returns
Type Parameters
T
The type of the property that changed.
Remarks
This method is just like SetProperty<T>(ref T, T, string?), just with the addition
of the validate
parameter. If that is set to true, the new value will be
validated and ErrorsChanged will be raised if needed. Following the behavior of the base method,
the PropertyChanging and PropertyChanged events
are not raised if the current and new value for the target property are the same.
Exceptions
- ArgumentNullException
Thrown if
propertyName
is null.
SetProperty<T>(ref T, T, IEqualityComparer<T>, bool, string)
Compares the current and new values for a given property. If the value has changed, raises the PropertyChanging event, updates the property with the new value, then raises the PropertyChanged event. See additional notes about this overload in SetProperty<T>(ref T, T, bool, string).
protected bool SetProperty<T>(ref T field, T newValue, IEqualityComparer<T> comparer, bool validate, string propertyName = null)
Parameters
field
TThe field storing the property's value.
newValue
TThe property's value after the change occurred.
comparer
IEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
validate
boolIf true,
newValue
will also be validated.propertyName
string(optional) The name of the property that changed.
Returns
Type Parameters
T
The type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
comparer
orpropertyName
are null.
SetProperty<TModel, T>(T, T, IEqualityComparer<T>, TModel, Action<TModel, T>, bool, string)
Compares the current and new values for a given nested property. If the value has changed, raises the PropertyChanging event, updates the property and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<TModel, T>(T, T, IEqualityComparer<T>, TModel, Action<TModel, T>, string?), with the difference being that this method is used to relay properties from a wrapped model in the current instance. For more info, see the docs for SetProperty<TModel, T>(T, T, IEqualityComparer<T>, TModel, Action<TModel, T>, string?).
protected bool SetProperty<TModel, T>(T oldValue, T newValue, IEqualityComparer<T> comparer, TModel model, Action<TModel, T> callback, bool validate, string propertyName = null) where TModel : class
Parameters
oldValue
TThe current property value.
newValue
TThe property's value after the change occurred.
comparer
IEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
model
TModelThe model
callback
Action<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
validate
boolIf true,
newValue
will also be validated.propertyName
string(optional) The name of the property that changed.
Returns
Type Parameters
TModel
The type of model whose property (or field) to set.
T
The type of property (or field) to set.
Exceptions
- ArgumentNullException
Thrown if
comparer
,model
,callback
orpropertyName
are null.
SetProperty<TModel, T>(T, T, TModel, Action<TModel, T>, bool, string)
Compares the current and new values for a given nested property. If the value has changed, raises the PropertyChanging event, updates the property and then raises the PropertyChanged event. The behavior mirrors that of SetProperty<TModel, T>(T, T, TModel, Action<TModel, T>, string?), with the difference being that this method is used to relay properties from a wrapped model in the current instance. For more info, see the docs for SetProperty<TModel, T>(T, T, TModel, Action<TModel, T>, string?).
protected bool SetProperty<TModel, T>(T oldValue, T newValue, TModel model, Action<TModel, T> callback, bool validate, string propertyName = null) where TModel : class
Parameters
oldValue
TThe current property value.
newValue
TThe property's value after the change occurred.
model
TModelThe model
callback
Action<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
validate
boolIf true,
newValue
will also be validated.propertyName
string(optional) The name of the property that changed.
Returns
Type Parameters
TModel
The type of model whose property (or field) to set.
T
The type of property (or field) to set.
Exceptions
- ArgumentNullException
Thrown if
model
,callback
orpropertyName
are null.
TrySetProperty<T>(T, T, Action<T>, out IReadOnlyCollection<ValidationResult>, string)
Tries to validate a new value for a specified property. If the validation is successful, SetProperty<T>(T, T, Action<T>, string?) is called, otherwise no state change is performed.
protected bool TrySetProperty<T>(T oldValue, T newValue, Action<T> callback, out IReadOnlyCollection<ValidationResult> errors, string propertyName = null)
Parameters
oldValue
TThe current property value.
newValue
TThe property's value after the change occurred.
callback
Action<T>A callback to invoke to update the property value.
errors
IReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyName
string(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
T
The type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
callback
orpropertyName
are null.
TrySetProperty<T>(T, T, IEqualityComparer<T>, Action<T>, out IReadOnlyCollection<ValidationResult>, string)
Tries to validate a new value for a specified property. If the validation is successful, SetProperty<T>(T, T, IEqualityComparer<T>, Action<T>, string?) is called, otherwise no state change is performed.
protected bool TrySetProperty<T>(T oldValue, T newValue, IEqualityComparer<T> comparer, Action<T> callback, out IReadOnlyCollection<ValidationResult> errors, string propertyName = null)
Parameters
oldValue
TThe current property value.
newValue
TThe property's value after the change occurred.
comparer
IEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
callback
Action<T>A callback to invoke to update the property value.
errors
IReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyName
string(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
T
The type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
comparer
,callback
orpropertyName
are null.
TrySetProperty<T>(ref T, T, IEqualityComparer<T>, out IReadOnlyCollection<ValidationResult>, string)
Tries to validate a new value for a specified property. If the validation is successful, SetProperty<T>(ref T, T, IEqualityComparer<T>, string?) is called, otherwise no state change is performed.
protected bool TrySetProperty<T>(ref T field, T newValue, IEqualityComparer<T> comparer, out IReadOnlyCollection<ValidationResult> errors, string propertyName = null)
Parameters
field
TThe field storing the property's value.
newValue
TThe property's value after the change occurred.
comparer
IEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
errors
IReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyName
string(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
T
The type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
comparer
orpropertyName
are null.
TrySetProperty<T>(ref T, T, out IReadOnlyCollection<ValidationResult>, string)
Tries to validate a new value for a specified property. If the validation is successful, SetProperty<T>(ref T, T, string?) is called, otherwise no state change is performed.
protected bool TrySetProperty<T>(ref T field, T newValue, out IReadOnlyCollection<ValidationResult> errors, string propertyName = null)
Parameters
field
TThe field storing the property's value.
newValue
TThe property's value after the change occurred.
errors
IReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyName
string(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
T
The type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
propertyName
is null.
TrySetProperty<TModel, T>(T, T, IEqualityComparer<T>, TModel, Action<TModel, T>, out IReadOnlyCollection<ValidationResult>, string)
Tries to validate a new value for a specified property. If the validation is successful, SetProperty<TModel, T>(T, T, IEqualityComparer<T>, TModel, Action<TModel, T>, string?) is called, otherwise no state change is performed.
protected bool TrySetProperty<TModel, T>(T oldValue, T newValue, IEqualityComparer<T> comparer, TModel model, Action<TModel, T> callback, out IReadOnlyCollection<ValidationResult> errors, string propertyName = null) where TModel : class
Parameters
oldValue
TThe current property value.
newValue
TThe property's value after the change occurred.
comparer
IEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
model
TModelThe model
callback
Action<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
errors
IReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyName
string(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
TModel
The type of model whose property (or field) to set.
T
The type of property (or field) to set.
Exceptions
- ArgumentNullException
Thrown if
comparer
,model
,callback
orpropertyName
are null.
TrySetProperty<TModel, T>(T, T, TModel, Action<TModel, T>, out IReadOnlyCollection<ValidationResult>, string)
Tries to validate a new value for a specified property. If the validation is successful, SetProperty<TModel, T>(T, T, TModel, Action<TModel, T>, string?) is called, otherwise no state change is performed.
protected bool TrySetProperty<TModel, T>(T oldValue, T newValue, TModel model, Action<TModel, T> callback, out IReadOnlyCollection<ValidationResult> errors, string propertyName = null) where TModel : class
Parameters
oldValue
TThe current property value.
newValue
TThe property's value after the change occurred.
model
TModelThe model
callback
Action<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
errors
IReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyName
string(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
TModel
The type of model whose property (or field) to set.
T
The type of property (or field) to set.
Exceptions
- ArgumentNullException
Thrown if
model
,callback
orpropertyName
are null.
ValidateAllProperties()
Validates all the properties in the current instance and updates all the tracked errors. If any changes are detected, the ErrorsChanged event will be raised.
protected void ValidateAllProperties()
Remarks
Only public instance properties (excluding custom indexers) that have at least one ValidationAttribute applied to them will be validated. All other members in the current instance will be ignored. None of the processed properties will be modified - they will only be used to retrieve their values and validate them.
ValidateProperty(object?, string)
Validates a property with a specified name and a given input value. If any changes are detected, the ErrorsChanged event will be raised.
protected void ValidateProperty(object? value, string propertyName = null)
Parameters
value
objectThe value to test for the specified property.
propertyName
stringThe name of the property to validate.
Exceptions
- ArgumentNullException
Thrown when
propertyName
is null.
Events
ErrorsChanged
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged