ASP.NET MVC:使用没有IClientValidatable的属性实现客户端验证

Mar*_*ues 5 validation asp.net-mvc jquery-plugins

如何在没有实现的情况下使用客户端验证创建自定义验证属性IClientValidatable

System.ComponentModel.DataAnnotations.RequiredAttribute客户端如何验证?

这样做的原因是因为我在另一个项目中使用类中的对象作为视图中的模型,我不想添加System.Web.MVC对该项目的引用.

编辑以添加更多信息:

  • 我知道这IClientValidatable用于向HTML添加自定义属性,稍后将通过不引人注意的验证使用.

  • 我知道我需要添加javascript代码才能在客户端进行验证.

我不知道的是如何使用自定义验证属性中的信息向HTML添加必要的属性,以便进行不显眼的验证.

这是我的自定义验证属性:

public class RequiredGuidAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        Guid? guidValue = value as Guid?;

        if (guidValue == null)
            return false;

        return guidValue != Guid.Empty;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我应用了属性的属性:

    [RequiredGuid(ErrorMessageResourceType = typeof(ClientOrderResources), ErrorMessageResourceName = "RequiredShippingMethod")]
    public Guid ShippingMethodId
    {
        get { return GetProperty(ShippingMethodIdProperty); }
        set { SetProperty(ShippingMethodIdProperty, value); }
    }
Run Code Online (Sandbox Code Playgroud)

最后我在视图中为该属性渲染隐藏的输入Html.HiddenFor.

现在,如何从属性中获取错误消息以将其应用于HTML?我应该自己动手使用Reflection还是有更好的方法?

然后我如何告诉Html.HiddenFor使用该信息为HTML添加必要的属性?

Zac*_*ber 6

我们遇到了类似的问题.我们在创建帐户时使用了一个模型,该模型在其自定义属性上使用IClientValidatable.但是,我们创建了一个批处理帐户创建过程,该过程位于我们无法引用System.Web.Mvc的网站之外.因此,当我们调用Validator.TryValidateObject时,从IClientValidatable继承的任何自定义验证程序都只是跳过.以下是我们正在使用的未能在我们网站之外验证的内容:

public class AgeValidatorAttribute : ValidationAttribute, IClientValidatable
{
    public int AgeMin { get; set; }
    public int AgeMax { get; set; }

    public override bool IsValid(object value)
    {
        //run validation
    }
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = ErrorMessageString,
            ValidationType = "agevalidator"
        };

        rule.ValidationParameters["agemin"] = AgeMin;
        rule.ValidationParameters["agemax"] = AgeMax;

        yield return rule;
    }
Run Code Online (Sandbox Code Playgroud)

删除System.Web.Mvc要求我们也删除GetClientValidationRules和IClientValidatable引用.为了做到这一点并仍然进行客户端验证,我们必须创建一个新类:

public class AgeValidatorClientValidator : DataAnnotationsModelValidator<AgeValidatorAttribute>
{
    private readonly string _errorMessage;
    private readonly string _validationType;

    public AgeValidatorClientValidator(ModelMetadata metadata, ControllerContext context, AgeValidatorAttribute attribute)
        : base(metadata, context, attribute)
    {
        this._errorMessage = attribute.FormatErrorMessage(metadata.DisplayName);
        this._validationType = "agevalidator";
    }

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    {
        var rule = new ModelClientValidationRule
        {
            ErrorMessage = this._errorMessage,
            ValidationType = this._validationType
        };

        rule.ValidationParameters["agemin"] = base.Attribute.AgeMin;
        rule.ValidationParameters["agemax"] = base.Attribute.AgeMax;

        yield return rule;
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,它与以前完全相同,它只是使用DataAnnatotationsModelValidator而不是IClientValidatable完成的.我们需要做的另一个步骤是将DataAnnotationsModelValidator实际附加到atttribute,并且这是在Global.asax.cs Application_Start方法中完成的.

DataAnnotationsModelValidatorProvider.RegisterAdapter( typeof(AgeValidatorAttribute), typeof(AgeValidatorClientValidator));

现在您可以像使用普通属性一样使用它:

[AgeValidator(AgeMax = 110, AgeMin = 18, ErrorMessage = "The member must be between 18 and 110 years old")]
public string DateOfBirth { get; set; }
Run Code Online (Sandbox Code Playgroud)

我知道这个问题已经有一年了,但我昨天花了一整天,今天有一半时间试图解决这个问题.所以我希望这可以帮助那些遇到同样问题的人,如果OP尚未找到答案的话.

请注意,我在这篇文章中没有包含任何javascript,因为它不需要使用jQuery.validate对自定义验证规则的标准实现进行任何更改.


afr*_*fr0 0

除非实现 IClientValidatable,否则无法在客户端上进行自定义验证。为此,您还需要添加客户端脚本。

http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvccustomvalidation_topic3.aspx