Esempio n. 1
0
        /// <summary>
        /// 获取对象的某一项属性错误
        /// </summary>
        /// <param name="obj">待校验的对象</param>
        /// <param name="propertyName">待校验的属性名</param>
        /// <param name="validator">FluentValidation验证器</param>
        /// <returns>错误信息</returns>
        public static ValidationResult GetPropertyErrorMsg(object obj, string propertyName, IValidator validator = null)
        {
            if (string.IsNullOrWhiteSpace(propertyName) || obj == null)
            {
                return(new ValidationResult(string.Empty));
            }

            if (validator != null)
            {
                var context = new ValidationContext(obj, null, new MemberNameValidatorSelector(new[] { propertyName }));
                var result  = validator.Validate(context);

                var error = result.Errors.FirstOrDefault(x => x.PropertyName.Equals(propertyName))?.ErrorMessage;
                if (!string.IsNullOrEmpty(error))
                {
                    return(new ValidationResult(error));
                }
            }

            var vc = new System.ComponentModel.DataAnnotations.ValidationContext(obj, null, null)
            {
                MemberName = propertyName
            };
            var res = new List <ValidationResult>();

            Validator.TryValidateProperty(obj.GetType().GetProperty(propertyName)?.GetValue(obj, null), vc, res);
            if (res.Any())
            {
                return(new ValidationResult(string.Join(Environment.NewLine,
                                                        res.Select(r => r.ErrorMessage).ToArray())));
            }

            return(new ValidationResult(string.Empty));
        }
Esempio n. 2
0
        /// <summary>
        /// 获取对象的整体错误信息
        /// </summary>
        /// <param name="obj">待校验的对象</param>
        /// <param name="validator">FluentValidation验证器</param>
        /// <returns>错误信息</returns>
        public static ValidationResult GetErrorMsg(object obj, IValidator validator = null)
        {
            if (validator != null)
            {
                var result = validator.Validate(obj);

                if (result.Errors.Any())
                {
                    return(new ValidationResult(result.Errors.First().ErrorMessage));
                }
            }

            if (obj != null)
            {
                var properties = obj.GetType().GetProperties();
                foreach (var propertyInfo in properties)
                {
                    var validationAttribute = propertyInfo.GetCustomAttribute(typeof(ValidationAttribute));
                    if (validationAttribute != null)
                    {
                        var vc = new System.ComponentModel.DataAnnotations.ValidationContext(obj, null, null)
                        {
                            MemberName = propertyInfo.Name
                        };
                        var res = new List <ValidationResult>();
                        Validator.TryValidateProperty(obj.GetType().GetProperty(propertyInfo.Name)?.GetValue(obj, null),
                                                      vc, res);
                        if (res.Count > 0)
                        {
                            return(new ValidationResult(string.Join(Environment.NewLine,
                                                                    res.Select(r => r.ErrorMessage).ToArray())));
                        }
                    }
                }
            }

            return(new ValidationResult(string.Empty));
        }
 public IEnumerable <System.ComponentModel.DataAnnotations.ValidationResult> Validate(System.ComponentModel.DataAnnotations.ValidationContext validationContext)
 {
     yield return(new System.ComponentModel.DataAnnotations.ValidationResult("Fail", new[] { "Name2" }));
 }