gpt4 book ai didi

asp.net-mvc - 使用 DataAnnotationsModelValidatorProvider.RegisterAdapter 的 MVC 2 与 MVC 3 自定义验证属性

转载 作者:行者123 更新时间:2023-12-02 02:57:42 24 4
gpt4 key购买 nike

我读过一些帖子,但现在找不到它,因为在 MVC 3 中实际上并不需要创建验证器,只需要创建属性。这是真的?我确实说过,我发现该属性上有 IClientValidatable 令人困惑。那么,如果注释具有客户端脚本名称 (IClientValidatable) 和验证能力 (ValidationAttribute IsValid),DataAnnotationsModelValidator 类会做什么?

如果我不必在全局中向验证器注册属性,那就太好了。这可以做到吗?我读到了一些不好的建议吗?

编辑:有趣的是,我只是通过排除验证器来测试它,将所有逻辑放在 IsValid 中,效果很好。我想唯一可能缺少的是 Controller 上下文,但我不确定这在验证中是否有用。 IsValid 有 ValidationContext,如果我需要服务,它有 ServiceContainer。我在这里没有注意到任何真正的缺点吗?

编辑2:我将从这个示例中的验证器开始:http://blogs.msdn.com/b/simonince/archive/2010/06/04/conditional-validation-in-mvc.aspx

属性:

public class RequiredIfAttribute : ValidationAttribute, IClientValidatable
{
private RequiredAttribute innerAttribute = new RequiredAttribute();
public string DependentProperty { get; set; }
public object TargetValue { get; set; }

public RequiredIfAttribute(string dependentProperty, object targetValue)
{
this.DependentProperty = dependentProperty;
this.TargetValue = targetValue;
}

public override bool IsValid(object value)
{
return innerAttribute.IsValid(value);
}

public System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule modelClientValidationRule = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName),
ValidationType = "requiredifattribute"
};
modelClientValidationRule.ValidationParameters.Add("requiredifattribute", DependentProperty);
yield return modelClientValidationRule;
}
}

验证器:

public class RequiredIfValidator : DataAnnotationsModelValidator<RequiredIfAttribute>
{
public RequiredIfValidator(ModelMetadata metadata, ControllerContext context, RequiredIfAttribute attribute)
: base(metadata, context, attribute)
{
}

public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
{
return base.GetClientValidationRules();
}

public override IEnumerable<ModelValidationResult> Validate(object container)
{
var field = Metadata.ContainerType.GetProperty(Attribute.DependentProperty);
if (field != null)
{
var value = field.GetValue(container, null);
if ((value == null && Attribute.TargetValue == null) ||
(value.Equals(Attribute.TargetValue)))
{
if (!Attribute.IsValid(Metadata.Model))
yield return new ModelValidationResult { Message = ErrorMessage };
}
}
}
}

使用上面的当前代码,我需要在 Global.asax.cs 文件中注册:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(RequiredIfAttribute), typeof(RequiredIfValidator));

但是如果我将所有内容都移到属性中,则不必注册它:

public class RequiredIfAttribute : ValidationAttribute, IClientValidatable
{
private RequiredAttribute innerAttribute = new RequiredAttribute();
public string DependentProperty { get; set; }
public object TargetValue { get; set; }

public RequiredIfAttribute(string dependentProperty, object targetValue)
{
this.DependentProperty = dependentProperty;
this.TargetValue = targetValue;
}

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var field = validationContext.ObjectInstance.GetType().GetProperty(DependentProperty);
if (field != null)
{
var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
if ((dependentValue == null && TargetValue == null) ||
(dependentValue.Equals(TargetValue)))
{
if (!innerAttribute.IsValid(value))
return new ValidationResult(ErrorMessage);
}
}
return ValidationResult.Success;
}

public System.Collections.Generic.IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
ModelClientValidationRule modelClientValidationRule = new ModelClientValidationRule()
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName),
ValidationType = "requiredifattribute"
};
modelClientValidationRule.ValidationParameters.Add("requiredifattribute", DependentProperty);
yield return modelClientValidationRule;
}
}

最后一段代码替换所有其他代码是否有问题?为什么我要保留验证器类?

最佳答案

疯狂飞镖,

MVC3 中添加了 IClientValidatable 接口(interface)。

您的第二个示例显示了此新界面的有效使用。您是对的,它不必注册,它将提供必要的客户端验证规则,以及进行必要的服务器端验证。

来吧,享受它。

辅导员

关于asp.net-mvc - 使用 DataAnnotationsModelValidatorProvider.RegisterAdapter 的 MVC 2 与 MVC 3 自定义验证属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6495510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com