开发者

WPF: Validation with IDataErrorInfo and low reuseability with similar validation rules

开发者 https://www.devze.com 2023-01-01 16:28 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号