More information in Error Message

Topics: Developer Forum, Wish List Forum
Apr 23, 2008 at 12:13 AM
I needed more info in the error message than just the string message. So, I created a new class - ValidationError -

public class ValidationError
{
private string _message;
private string _propertyName;
private object _propertyValue;
public string Message
{
get { return _message; }
set { _message = value; }
}
public string PropertyName
{
get { return _propertyName; }
set { _propertyName = value; }
}
public object PropertyValue
{
get { return _propertyValue; }
set { _propertyValue = value; }
}
public string FormattedErrorText
{
get
{
return String.Format(_message, _propertyName, _propertyValue);
}
}
public ValidationError(string message, string propertyname, object propertyValue)
{
_message = message;
_propertyName = propertyname;
_propertyValue = propertyValue;
}
}
"}
and use this instead of the simple string error message.
A you can see, the error now contains the name of the property that was validated, and its value.
So my attributes can be something like
[ValidateMaxValue(1000, "{0} has a maximum value of 1000 so {1} is just toooooo big.")]
Which will display "PropertyName has a maximum value of 1000, so 1200 is just tooooo big" where the property name is PropertyName and its value is 1200.
This also allows me, in my GUI, to test the property name within the collection of errors, and set the appropriate control's properties on the form (to highlight errors, for example)
I haven't finished development of this yet - still doing some fine tuning and testing - but thought I would raise it in case there is any interest out there.
.\\axxx