MVVM - Validation

前端 未结 2 1572
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-28 08:42

We\'re trying to figure out validation in the mvvm doing validation in the business logic or model. I\'ve implemented the validate by exception type in our business logic -

2条回答
  •  粉色の甜心
    2020-12-28 09:26

    You could consider using the System.ComponentModel.IDataErrorInfo interface. This very handy interface gives you the ability to:

    • do validation in a MVVM compliant manner
    • do custom validation for any particular field (the validation could check several values if you want it to)
    • bind your UI to the validation errors

    You implement IDataErrorInfo on your viewmodel (or even virtually in your view model base, and override it in your derived view models). Due to the nature of databinding, the values i need to check are all there in the view model, and i can test any combination of them. Of course you still have your validation in your business layer, but you no longer need to make a trip to your business layer (or Model) just to effect some validation.

    Here is a quick example from a (WPF) screen that gathers some user details and does basic validation on them:

    C# code:

        #region IDataErrorInfo Members
    
        /// 
        /// Gets an error message indicating what is wrong with this object.
        /// 
        /// 
        /// An error message indicating what is wrong with this object. The default is an empty string ("").
        public override string Error
        {
            get
            {
                return this["UserCode"] + this["UserName"] + this["Password"] + this["ConfirmedPassword"] + this["EmailAddress"];
            }
        }
    
        /// 
        /// Gets the  with the specified column name.
        /// 
        /// 
        public override string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                    case "UserCode":
                        if (!string.IsNullOrEmpty(UserCode) && UserCode.Length > 20)
                            return "User Code must be less than or equal to 20 characters";
                        break;
    
                    case "UserName":
                        if (!string.IsNullOrEmpty(UserCode) && UserCode.Length > 60)
                            return "User Name must be less than or equal to 60 characters";
                        break;
    
                    case "Password":
                        if (!string.IsNullOrEmpty(Password) && Password.Length > 60)
                            return "Password must be less than or equal to 60 characters";
                        break;
    
                    case "ConfirmedPassword":
                        if (Password != ConfirmedPassword)
                            return Properties.Resources.ErrorMessage_Password_ConfirmedPasswordDoesntMatch; 
                        break;
    
                    case "EmailAddress":
                        if (!string.IsNullOrEmpty(EmailAddress))
                        {
                            var r = new Regex(_emailRegex);
                            if (!r.IsMatch(EmailAddress))
                                return Properties.Resources.ErrorMessage_Email_InvalidEmailFormat;
                        }
                        break;
                }
                return string.Empty;
            }
        }
    
        #endregion
    

    and here is the XAML markup for two of the textboxes on the page (note particularly the ValidatesOnDataErrors and ValidatesOnExceptions properties in the Text binding):

    
    
    
    

提交回复
热议问题