I'm trying to find the best way to validate data in MVVM. Currently, I'm trying to use IDataErrorInfo with Data Annotations using the MVVM pattern.
However, nothing seems to work and I'm not sure what I could be doing wrong. I have something like this.
Model
public class Person : IDataErrorInfo
{
[Required(ErrorMessage="Please enter your name")]
public string Name { get; set; }
public string Error
{
get { throw new NotImplementedException(); }
}
string IDataErrorInfo.this[string propertyName]
{
get
{
return OnValidate(propertyName);
}
}
protected virtual string OnValidate(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException("Property may not be null or empty", propertyName);
string error = string.Empty;
var value = this.GetType().GetProperty(propertyName).GetValue(this, null);
var results = new List<ValidationResult>();
var context = new ValidationContext(this, null, null) { MemberName = propertyName };
var result = Validator.TryValidateProperty(value, context, results);
if(!result)
{
var validationResult = results.First();
error = validationResult.ErrorMessage;
}
return error;
}
}
Model code courtesy of the solution at How to catch DataAnnotations Validation in MVVM (This answer does not meet my criteria unfortunately.)
ViewModel
public class PersonViewModel
{
private Person _person;
public string Name
{
get
{
return _person.Name
}
set
{
_person.Name = value;
}
}
}
View
<Label Content="Name:" />
<TextBox Text="{Binding UpdateSourceTrigger=LostFocus,
Path=Name,
ValidatesOnDataErrors=True,
NotifyOnValidationError=true}" />
Is there any way to keep the seperation between model, view, and viewmodel while still utilizing data annotations for validation with IDataErrorInfo?