如何告知Data Annotations验证器还验证复杂的子属性?

时间:2010-03-22 16:10:09

标签: c# validation data-annotations

我可以在验证父对象时自动验证复杂的子对象,并将结果包含在已填充的ICollection<ValidationResult>中吗?

如果我运行以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ConsoleApplication1
{
    public class Person
    {
        [Required]
        public string Name { get; set; }

        public Address Address { get; set; }
    }

    public class Address
    {
        [Required]
        public string Street { get; set; }

        [Required]
        public string City { get; set; }

        [Required]
        public string State { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person person = new Person
            {
                Name = null,
                Address = new Address
                {
                    Street = "123 Any St",
                    City = "New York",
                    State = null
                }
            };

            var validationContext = new ValidationContext(person, null, null);
            var validationResults = new List<ValidationResult>();

            var isValid = Validator.TryValidateObject(person, validationContext, validationResults);

            Console.WriteLine(isValid);

            validationResults.ForEach(r => Console.WriteLine(r.ErrorMessage));

            Console.ReadKey(true);
        }
    }
}

我得到以下输出:

  

False
  The Name field is required.

但我期待类似的东西:

  

False
  The Name field is required.
  The State field is required.


我提供了一个更好的子对象验证解决方案的赏金,但没有得到任何接受者,理想情况下

  • 将子对象验证为任意深度
  • 处理每个对象的多个错误
  • 正确识别子对象字段上的验证错误。

我仍然对框架不支持这一点感到惊讶。

3 个答案:

答案 0 :(得分:25)

问题 - 模型活页夹订单

不幸的是,这是Validator.TryValidateObject的标准行为

  

不会递归验证对象的属性值

正如Jeff Handley关于Validating Object and Properties with the Validator的文章所指出的,默认情况下,验证器将按顺序验证:

  1. 属性级属性
  2. 对象级属性
  3. 模型级实施IValidatableObject
  4. 问题是,在每一步......

      

    如果任何验证器无效,Validator.ValidateObject将中止验证并返回失败

    问题 - 模型粘合剂字段

    另一个可能的问题是模型绑定器仅对已决定绑定的对象运行验证。例如,如果不为模型上的复杂类型中的字段提供输入,则模型绑定器根本不需要检查这些属性,因为它没有在这些对象上调用构造函数。根据Brad Wilson关于Input Validation vs. Model Validation in ASP.NET MVC的伟大文章:

      

    我们没有递归地“潜入”Address对象的原因是表单中没有任何内容绑定Address中的任何值。

    解决方案 - 在“属性”

    的同时验证对象

    解决此问题的一种方法是通过向属性添加自定义验证属性,将对象级验证转换为属性级别验证,该属性将返回对象本身的验证结果。

    Josh Carroll关于Recursive Validation Using DataAnnotations的文章提供了一个这样的策略的实现(最初在this SO question)。如果我们想要验证复杂类型(如Address),我们可以向属性添加自定义ValidateObject属性,因此在第一步中对其进行评估

    public class Person {
      [Required]
      public String Name { get; set; }
    
      [Required, ValidateObject]
      public Address Address { get; set; }
    }
    

    您需要添加以下 ValidateObjectAttribute 实现:

    public class ValidateObjectAttribute: ValidationAttribute {
       protected override ValidationResult IsValid(object value, ValidationContext validationContext) {
          var results = new List<ValidationResult>();
          var context = new ValidationContext(value, null, null);
    
          Validator.TryValidateObject(value, context, results, true);
    
          if (results.Count != 0) {
             var compositeResults = new CompositeValidationResult(String.Format("Validation for {0} failed!", validationContext.DisplayName));
             results.ForEach(compositeResults.AddResult);
    
             return compositeResults;
          }
    
          return ValidationResult.Success;
       }
    }
    
    public class CompositeValidationResult: ValidationResult {
       private readonly List<ValidationResult> _results = new List<ValidationResult>();
    
       public IEnumerable<ValidationResult> Results {
          get {
             return _results;
          }
       }
    
       public CompositeValidationResult(string errorMessage) : base(errorMessage) {}
       public CompositeValidationResult(string errorMessage, IEnumerable<string> memberNames) : base(errorMessage, memberNames) {}
       protected CompositeValidationResult(ValidationResult validationResult) : base(validationResult) {}
    
       public void AddResult(ValidationResult validationResult) {
          _results.Add(validationResult);
       }
    }
    

    解决方案 - 与属性

    同时验证模型

    对于实现IValidatableObject的对象,当我们检查ModelState时,我们还可以在返回错误列表之前检查模型本身是否有效。我们可以通过调用ModelState.AddModelError(field, error)添加我们想要的任何错误。正如How to force MVC to Validate IValidatableObject中所述,我们可以这样做:

    [HttpPost]
    public ActionResult Create(Model model) {
        if (!ModelState.IsValid) {
            var errors = model.Validate(new ValidationContext(model, null, null));
            foreach (var error in errors)                                 
                foreach (var memberName in error.MemberNames)
                    ModelState.AddModelError(memberName, error.ErrorMessage);
    
            return View(post);
        }
    }
    
      

    ,如果您想要更优雅的解决方案,可以通过在{_ 1}}的Application_Start()中提供自己的自定义模型绑定器实现来编写代码一次。有很好的实施herehere

答案 1 :(得分:9)

我也碰到了这个,发现了这个帖子。这是第一遍:

namespace Foo
{
    using System.ComponentModel.DataAnnotations;
    using System.Linq;

    /// <summary>
    /// Attribute class used to validate child properties.
    /// </summary>
    /// <remarks>
    /// See: http://stackoverflow.com/questions/2493800/how-can-i-tell-the-data-annotations-validator-to-also-validate-complex-child-pro
    /// Apparently the Data Annotations validator does not validate complex child properties.
    /// To do so, slap this attribute on a your property (probably a nested view model) 
    /// whose type has validation attributes on its properties.
    /// This will validate until a nested <see cref="System.ComponentModel.DataAnnotations.ValidationAttribute" /> 
    /// fails. The failed validation result will be returned. In other words, it will fail one at a time. 
    /// </remarks>
    public class HasNestedValidationAttribute : ValidationAttribute
    {
        /// <summary>
        /// Validates the specified value with respect to the current validation attribute.
        /// </summary>
        /// <param name="value">The value to validate.</param>
        /// <param name="validationContext">The context information about the validation operation.</param>
        /// <returns>
        /// An instance of the <see cref="T:System.ComponentModel.DataAnnotations.ValidationResult"/> class.
        /// </returns>
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var isValid = true;
            var result = ValidationResult.Success;

            var nestedValidationProperties = value.GetType().GetProperties()
                .Where(p => IsDefined(p, typeof(ValidationAttribute)))
                .OrderBy(p => p.Name);//Not the best order, but at least known and repeatable.

            foreach (var property in nestedValidationProperties)
            {
                var validators = GetCustomAttributes(property, typeof(ValidationAttribute)) as ValidationAttribute[];

                if (validators == null || validators.Length == 0) continue;

                foreach (var validator in validators)
                {
                    var propertyValue = property.GetValue(value, null);

                    result = validator.GetValidationResult(propertyValue, new ValidationContext(value, null, null));
                    if (result == ValidationResult.Success) continue;

                    isValid = false;
                    break;
                }

                if (!isValid)
                {
                    break;
                }
            }
            return result;
        }
    }
}

答案 2 :(得分:5)

您需要创建自己的验证属性(例如,[CompositeField])来验证子属性。