9

我创建了一个以类为目标的自定义 ValidationAttribute。每当我尝试调用 Validator.TryValidateObject 时,这都会正确验证。但是当我的类内的属性中有其他 ValidationAttribute 时,验证结果不包含类级别验证的结果。

这是一个示例代码:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class IsHelloWorldAttribute : ValidationAttribute
{
    public object _typeId = new object();
    public string FirstProperty { get; set; }
    public string SecondProperty { get; set; }

    public IsHelloWorldAttribute(string firstProperty, string secondProperty)
    {
        this.FirstProperty = firstProperty;
        this.SecondProperty = secondProperty; 
    }

    public override bool IsValid(object value)
    {
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        string str1 = properties.Find(FirstProperty, true).GetValue(value) as string;
        string str2 = properties.Find(SecondProperty, true).GetValue(value) as string;

        if (string.Format("{0}{1}", str1,str2) == "HelloWorld")
            return true;
        return false;
    }

    public override object TypeId
    {
        get
        {
            return _typeId;
        }
    }
}

这是我需要验证的类的代码

[IsHelloWorld("Name", "Code", ErrorMessage="Is not Hello World")]
public class MyViewModel : BaseViewModel
{
    string name;
    string code;

    [Required]
    public string Name
    {
        get { return model.Name; }
        set
        {
            if (model.Name != value)
            {
                model.Name = value;
                base.RaisePropertyChanged(() => this.Name);
            }
        }
    }        

    public string Code
    {
        get { return code; }
        set
        {
            if (code != value)
            {
                code = value;
                base.RaisePropertyChanged(() => this.Code);
            }
        }
    }
}

下面是我如何调用 TryValidateObject 方法:

            var validationContext = new ValidationContext(this, null, null);               
            var validationResults = new List<ValidationResult>();               
            Validator.TryValidateObject(this, validationContext, validationResults, true);

现在,如果我在 Name 属性中有 [Required] 属性并且我尝试调用 Validator.TryValidateObject,则验证结果只有一个,这就是Required 验证的结果。但是当我从 Name 中删除 [Required] 属性并留下 IsHellowWorld 属性然后调用 TryValidateObject 时,它会给我一个结果,这就是 HellowWorldValidation 的结果。

我需要做的是在类级别和属性级别上进行所有验证。我可以在不实现自己的 TryValidateObject 方法的情况下实现这一目标吗?

4

1 回答 1

9

这是因为如果检测到属性错误,检查就会短路。这是有道理的,因为类级别的验证可能会更昂贵,可能涉及回调、其他数据源调用等。

如果检测到一个属性错误,那么它的逻辑就会停止。

在 System.ComponentModel.DataAnnotations.Validator 源代码中:

public static bool TryValidateObject(object instance, ValidationContext validationContext, 
  ICollection<ValidationResult> validationResults, bool validateAllProperties)
{
  if (instance == null)
    throw new ArgumentNullException("instance");
  if (validationContext != null && instance != validationContext.ObjectInstance)
    throw new ArgumentException(DataAnnotationsResources.
    Validator_InstanceMustMatchValidationContextInstance, "instance");
  bool flag = true;
  bool breakOnFirstError = validationResults == null;
  foreach (Validator.ValidationError validationError in 
    Validator.GetObjectValidationErrors(instance, validationContext, 
    validateAllProperties, breakOnFirstError))
  {
    flag = false;
    if (validationResults != null)
      validationResults.Add(validationError.ValidationResult);
  }
  return flag;
}

请注意Validator.GetObjectValidationErrors依次定义为的调用:

private static IEnumerable<Validator.ValidationError> GetObjectValidationErrors(
  object instance, ValidationContext validationContext, 
  bool validateAllProperties, bool breakOnFirstError)
{
  if (instance == null)
    throw new ArgumentNullException("instance");
  if (validationContext == null)
    throw new ArgumentNullException("validationContext");
  List<Validator.ValidationError> list = new List<Validator.ValidationError>();

  //Check for property errors here
  list.AddRange(Validator.GetObjectPropertyValidationErrors(instance, validationContext,
    validateAllProperties, breakOnFirstError));

  // Short circuits here if any found
  if (Enumerable.Any<Validator.ValidationError>(
    (IEnumerable<Validator.ValidationError>) list))
    return (IEnumerable<Validator.ValidationError>) list;

  // Class level validation occurs below this point
  IEnumerable<ValidationAttribute> validationAttributes = 
    Validator._store.GetTypeValidationAttributes(validationContext);
  list.AddRange(Validator.GetValidationErrors(instance, validationContext, 
    validationAttributes, breakOnFirstError));
  if (Enumerable.Any<Validator.ValidationError>(
    (IEnumerable<Validator.ValidationError>) list))
    return (IEnumerable<Validator.ValidationError>) list;
  IValidatableObject validatableObject = instance as IValidatableObject;
  if (validatableObject != null)
  {
    foreach (ValidationResult validationResult in 
      Enumerable.Where<ValidationResult>(validatableObject.Validate(validationContext), 
      (Func<ValidationResult, bool>) (r => r != ValidationResult.Success)))
      list.Add(new Validator.ValidationError((ValidationAttribute) null, instance, 
        validationResult));
  }
  return (IEnumerable<Validator.ValidationError>) list;
}
于 2015-08-11T07:50:31.200 回答