ASP.NET MVC3 System.ComponentModel.DataAnnotations - 注释可以确定它正在验证的类吗?

时间:2012-06-14 06:55:04

标签: c# asp.net-mvc-3 data-annotations

我正在使用“扩展验证”属性来启用更严格的验证,具体取决于我如何编辑记录。

EG:我有一个有子对象的Parent。

编辑子项时,ModelState.isValid为false,因为父项缺失,例如名字,因为我只按ID选择父项。

当我编辑父母时 - 我希望这些字段是必需的,但是在编辑例如孩子时并不是这样,而且关联的父母是构成孩子的一部分。

我有以下代码:

public class Parent
{
    [Required]        
    public virtual int Id { get; set; }


    [ExtendedValidationRequired(typeof(Parent))]
    public virtual string Name { get; set; }
    ....}

以下用于验证:

public static class ExtendedValidation
{
    private static Dictionary<Type, bool> extendedValidationExemptions = new Dictionary<Type, bool>();


    /// <summary>
    /// Disable extended validation for a specific type
    /// </summary>
    /// <param name="type"></param>
    public static void DisableExtendedValidation(Type type)
    {
        extendedValidationExemptions[type] = true;
    }
    /// <summary>
    /// Clear any EV exemptions
    /// </summary>
    public static void Reset()
    {
        extendedValidationExemptions.Clear();
    }

    /// <summary>
    /// Check if a class should perform extended validation
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static bool IsExtendedValidationEnabled(Type type)
    {
        if (extendedValidationExemptions.ContainsKey(type))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}






[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]

public class ExtendedValidationRequiredAttribute : RequiredAttribute
{


    private Type _type;
    // Summary:
    //     Initializes a new instance of the System.ComponentModel.DataAnnotations.RequiredAttribute
    //     class.
    public ExtendedValidationRequiredAttribute(Type type)
    {
        _type = type;
    }



    // Summary:
    //     Checks that the value of the required data field is not empty.
    //
    // Parameters:
    //   value:
    //     The data field value to validate.
    //
    // Returns:
    //     true if validation is successful; otherwise, false.
    //
    // Exceptions:
    //   System.ComponentModel.DataAnnotations.ValidationException:
    //     The data field value was null.
    public override bool IsValid(object value)
    {
        if (ExtendedValidation.IsExtendedValidationEnabled(_type))
        {
            return base.IsValid(value);
        }
        else
        {
            return true;
        }


    }




}

有什么方法可以让我明确地将类型传递给验证器?我希望能够以编程方式启用/禁用这些验证属性。

例如,如果我正在编辑Child,但是Parent的[Required]字段会导致我遇到问题,我会在extendedValidator中禁用typeof(Parent),然后验证应该正常工作。我只是担心所有typeof()的性能影响。

我试图解决的问题是我在编辑类

公共课儿童    {

[必需]    public virtual int id {get; set;}

[必需]    public virtual Parent parent {get; set;}

....    }

我的表单只有父ID(这是我的EF或hibernate应用程序所需的全部内容),但它不会验证,除非在表单中传递的Parent具有所有必需字段 - 这相当浪费。

1 个答案:

答案 0 :(得分:1)

您可以覆盖ValidationAttribute.IsValid Method (Object, ValidationContext)

protected virtual ValidationResult IsValid(
    Object value,
    ValidationContext validationContext
)

访问ValidationContext Class

的成员