ObservableValidator with subclasses #557
-
i have main view model also two classes in this viewmodel always false if i send FullName blank
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You're trying to validate |
Beta Was this translation helpful? Give feedback.
-
@Sergio0694 How to implement it? I tried to write a public class ForwardErrorsAttribute : ValidationAttribute
{
public override bool IsValid(object? value)
{
return !((INotifyDataErrorInfo)value).HasErrors;
}
} But the code will not be executed. Update Edit: I found a solution: [ForwardErrors]
public SubclassProperty SubclassProperty
{
get => _subclassProperty;
set
{
if (EqualityComparer<SubclassProperty>.Default.Equals(_subclassProperty, value))
{
return;
}
OnPropertyChanging();
// Unregister oldValue events。
_subclassProperty.PropertyChanging -= SubclassPropertyChanging;
_subclassProperty.PropertyChanged -= SubclassPropertyChanged;
_subclassProperty.ErrorsChanged -= SubclassProperty_ErrorsChanged;
// Ensure newValue register events。
value.ErrorsChanged -= SubclassProperty_ErrorsChanged;
value.ErrorsChanged += SubclassProperty_ErrorsChanged;
value.PropertyChanging -= SubclassPropertyChanging;
value.PropertyChanging += SubclassPropertyChanging;
value.PropertyChanged -= SubclassPropertyChanged;
value.PropertyChanged += SubclassPropertyChanged;
_subclassProperty= value;
OnPropertyChanged();
}
}
private void SubclassProperty_ErrorsChanged(object? sender, DataErrorsChangedEventArgs e)
{
ValidateProperty(SubclassProperty, nameof(SubclassProperty));
} |
Beta Was this translation helpful? Give feedback.
You're trying to validate
TestViewModel
, but this type has no validatable properties. Validation doesn't automatically handle nested properties, you'll have to either call the validation on both nested properties manually and forward the errors, or write some custom attribute that will implement the validation for a nested viewmodel. The fact you're currently not seeing errors is expected 🙂