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
itemsIDictionary<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
validationContextValidationContextThe 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
validationContextis 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
serviceProviderIServiceProviderAn IServiceProvider instance to make available during validation.
itemsIDictionary<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
propertyNamestringThe 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
propertyNamestring
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
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
callbackAction<T>A callback to invoke to update the property value.
validateboolIf true,
newValuewill also be validated.propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe 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
callbackorpropertyNameare 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
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
callbackAction<T>A callback to invoke to update the property value.
validateboolIf true,
newValuewill also be validated.propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
comparer,callbackorpropertyNameare 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
fieldTThe field storing the property's value.
newValueTThe property's value after the change occurred.
validateboolIf true,
newValuewill also be validated.propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe 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
propertyNameis 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
fieldTThe field storing the property's value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
validateboolIf true,
newValuewill also be validated.propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TThe type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
comparerorpropertyNameare 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
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
modelTModelThe model
callbackAction<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
validateboolIf true,
newValuewill also be validated.propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TModelThe type of model whose property (or field) to set.
TThe type of property (or field) to set.
Exceptions
- ArgumentNullException
Thrown if
comparer,model,callbackorpropertyNameare 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
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
modelTModelThe model
callbackAction<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
validateboolIf true,
newValuewill also be validated.propertyNamestring(optional) The name of the property that changed.
Returns
Type Parameters
TModelThe type of model whose property (or field) to set.
TThe type of property (or field) to set.
Exceptions
- ArgumentNullException
Thrown if
model,callbackorpropertyNameare 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
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
callbackAction<T>A callback to invoke to update the property value.
errorsIReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyNamestring(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
TThe type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
callbackorpropertyNameare 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
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
callbackAction<T>A callback to invoke to update the property value.
errorsIReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyNamestring(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
TThe type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
comparer,callbackorpropertyNameare 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
fieldTThe field storing the property's value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
errorsIReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyNamestring(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
TThe type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
comparerorpropertyNameare 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
fieldTThe field storing the property's value.
newValueTThe property's value after the change occurred.
errorsIReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyNamestring(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
TThe type of the property that changed.
Exceptions
- ArgumentNullException
Thrown if
propertyNameis 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
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
comparerIEqualityComparer<T>The IEqualityComparer<T> instance to use to compare the input values.
modelTModelThe model
callbackAction<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
errorsIReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyNamestring(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
TModelThe type of model whose property (or field) to set.
TThe type of property (or field) to set.
Exceptions
- ArgumentNullException
Thrown if
comparer,model,callbackorpropertyNameare 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
oldValueTThe current property value.
newValueTThe property's value after the change occurred.
modelTModelThe model
callbackAction<TModel, T>The callback to invoke to set the target property value, if a change has occurred.
errorsIReadOnlyCollection<ValidationResult>The resulting validation errors, if any.
propertyNamestring(optional) The name of the property that changed.
Returns
- bool
Whether the validation was successful and the property value changed as well.
Type Parameters
TModelThe type of model whose property (or field) to set.
TThe type of property (or field) to set.
Exceptions
- ArgumentNullException
Thrown if
model,callbackorpropertyNameare 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
valueobjectThe value to test for the specified property.
propertyNamestringThe name of the property to validate.
Exceptions
- ArgumentNullException
Thrown when
propertyNameis null.
Events
ErrorsChanged
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged