MVC3自定义不显眼验证 - 从复选框强制验证

时间:2011-08-25 18:29:59

标签: asp.net-mvc-3 jquery-validate unobtrusive-validation

我有以下用于自定义验证的类:

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false, Inherited=true)]
public sealed class RequiredIfAnyTrueAttribute : ValidationAttribute, IClientValidatable
{
    private const string DefaultErrorMessage = "{0} is required";

    public List<string> OtherProperties { get; private set; }

    public RequiredIfAnyTrueAttribute(string otherProperties)
        : base(DefaultErrorMessage)
    {
        if (string.IsNullOrEmpty(otherProperties))
            throw new ArgumentNullException("otherProperty");

        OtherProperties = new List<string>(otherProperties.Split(new char[] { '|', ',' }));
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(ErrorMessageString, name);
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (value == null)
        {
            foreach (string s in OtherProperties)
            {
                var otherProperty = validationContext.ObjectType.GetProperty(s);
                var otherPropertyValue = otherProperty.GetValue(validationContext.ObjectInstance, null);

                if (otherPropertyValue.Equals(true))
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
            }
        }

        return ValidationResult.Success;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var clientValidationRule = new ModelClientValidationRule()
        {
            ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
            ValidationType = "requiredifanytrue"
        };

        clientValidationRule.ValidationParameters.Add("otherproperties", string.Join("|",OtherProperties));

        return new[] { clientValidationRule };
    }
}

我的ViewModel是:

public class SampleViewModel
{
    public bool PropABC { get; set; }
    public bool PropXYZ { get; set; }

    [RequiredIfAnyTrue("PropABC|PropXYZ")]
    public int? TestField { get; set; }
}

当我的强类型视图渲染时,一切看起来都能正常工作。如果选择PropABC或PropXYZ,则我需要输入TestField的值。客户端和服务器端验证都是有效的。

但是,鉴于以下事件顺序:

  1. 检查PropABC
  2. 提交表格
  3. 需要TestField的客户端验证
  4. 取消选中PropABC
  5. 客户端验证不会重新触发和验证消息 保留至表格提交
  6. 为了解决#5,我通常会通过jquery onready将点击事件附加到复选框以重新启用验证。

    在给定MVC3 + unobstrusive + jquery的情况下,是否有首选/推荐的方法来手动强制进行客户端验证?

3 个答案:

答案 0 :(得分:1)

您需要编写自己的属性吗?如果不是,我认为你可以避免“重新发明轮子”

FoolProof效果很好。你得到它作为NuGet包。

NuGet: install-package foolproof

它包含许多greate属性,用于动态所需字段的各种组合等。

答案 1 :(得分:1)

Shawn,附加到事件是获得验证的最佳方法。

我建议创建一个名为“validate”的类(或类似的东西),将其添加到要验证的每个元素,然后使用jQuery附加到click和blur事件(可能还有更改事件)具有该类的每个元素,并像这样验证元素:

$("form").validate().element(this);

答案 2 :(得分:0)

FoolProof仍处于测试阶段,不适用于嵌套的viewmodel,也适用于数组

相关问题