WPF: Validation with IDataErrorInfo and low reuseability with similar validation rules
I have a pupil entity implementing IDataErrorInfo:
Now the exact same rules I want to validate against the Lastname, Gender, Street, City, Postal and Phone.
Do I really have to repeat all that ? Using ValidationRule class would be better but then I
can not handle disabling/enabling buttons via ICommand.
...
#region Validation Rules
private string ValidateFirstName()
{
if (IsStringMissing(this.FirstName))
return ErrorStrings.General_Err开发者_开发问答or_StringMustNotBeEmpty;
if (IsStringTooLong(this.FirstName))
return ErrorStrings.General_Error_StringTooLong50Maximum;
return null;
}
private static bool IsStringMissing(string value)
{
return String.IsNullOrEmpty(value) || value.Trim() == String.Empty;
}
private static bool IsStringTooLong(string value)
{
return value.Length > 50;
}
#endregion
I think it is easier to use attributes. Look at my answer to this question:
How can I define a IDataErrorInfo Error Property for multiple BO properties
精彩评论