I'm starting to implement validation in my WPF project via the IDataErrorInfo interface. My business object contains multiple properties with validation info. How do I get a list of ALL the error messages associated with the object. My thought is that thats what the Error property is for, but I cannot track down anyone using this for reporting on multiple properties.
Thanks!
public string this[string property]
{
get {
string msg = null;
switch (property)
{
case "LastName":
if (string.IsNullOrEmpty(LastName))
msg = "Need a last name";
break;
case "FirstName":
if (string.IsNullOrEmpty(LastName))
msg = "Need a first name";
break;
default:
throw new ArgumentException(
"Unrecognized property: " + property);
}
return msg;
}
}
public string Error
{
get
{
return null ;
}
}
Yeah, I see where you could use the indexer. Not a bad way to go I guess. I was really focused on the 'Error' property though. I like the notion of having the errors contained within the business object. I think what I want to do doesnt exist natively, so I just created a dictionary of errors (updated anytime a property changes) on the object and let the Error return a CarriageReturn delimited list of errors, like so :
My understanding is that to use this interface, you enumerate the properties on the object, and call the indexer once for each property. It is the caller's responsibility to aggregate any error messages.
I think it is much easier to use the Validation attributes.
The helper class InputValidation looks like this