客户端列表的MVC自定义验证

时间:2015-03-13 15:34:40

标签: asp.net-mvc asp.net-mvc-4

我试图编写一个在客户端工作的自定义验证器,验证所有复选框都已勾选。

这是模型上的声明:

[DeclarationsAccepted(ErrorMessage = "You must tick all declarations")]        
    public IList<DeclarationQuestion> DeclarationQuestions { get; set; }

以下是属性:

public class DeclarationsAccepted : ValidationAttribute, IClientValidatable
{
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {

        var questions = value as IList<DeclarationQuestion>;

        if (questions != null && questions.All(c => c.Answer))
        {
            return ValidationResult.Success;
        }
        return new ValidationResult("You must accepted all declarations to continue");
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var modelClientValidationRule = new ModelClientValidationRule
        {
            ValidationType = "declarationsaccepted",
            ErrorMessage = FormatErrorMessage(metadata.DisplayName)
        };            
        yield return modelClientValidationRule;
    }
}

到目前为止这么好,服务器方面。

对于客户,我按照以下方式进行接线:

jQuery.validator.addMethod('declarationsaccepted', function (value, element, params) {

        //Implement logic here to check all boxes are ticked
        console.log(value);

        return false;
    }, '');


    jQuery.validator.unobtrusive.adapters.add('declarationsaccepted', {}, function (options) {
        options.rules['declarationsaccepted'] = true;
        options.messages['declarationsaccepted'] = options.message;
    });

我显示如下所示的复选框:

@{ var questionIndex = 0; }
        @foreach (var question in Model.DeclarationQuestions)
        {

                @Html.CheckBoxFor(model => Model.DeclarationQuestions[questionIndex].Answer, new { id = "DeclarationQuestions" + questionIndex})

            questionIndex++;
        }

然后使用以下方法显示验证消息:

@Html.ValidationMessageFor(c => c.DeclarationQuestions)

当我提交表单时,会显示消息,但只有在回发到服务器后才会显示。有没有办法让这个在客户端工作?

1 个答案:

答案 0 :(得分:5)

您无法获得客户端验证的原因是因为html帮助程序为与属性关联的控件生成data-val-*属性。 jquery.validate.unobtrusive在解析表单时读取这些属性并使用规则,在与该控件关联的ValidationMessageFor()生成的相应元素中显示错误消息(通过匹配id属性来实现此目的元素 - 错误消息是在<span for="TheIdOfTheAssociatedControl" ...>)的范围内生成的。

您不能(并且无法)为属性DeclarationQuestions生成控件(仅适用于DeclarationQuestions中每个项目的属性,因此没有任何内容可以匹配。

您可以通过添加自己的错误消息占位符并拦截.submit事件来处理此问题

html(将css添加到样式#conditions-error作为display:none;

<span id="delarations-error" class="field-validation-error">
  <span>You must accept all declarations to continue.</span>
</span>

脚本

var declarationsError = $('#delarations-error');
$('form').submit(function() {
  var isValid = $('.yourCheckBoxClass').not(':checked') == 0;
  if(!isValid) {
    declarationsError.show(); // display error message
    return false; // prevent submit
  }
});

$('.yourCheckBoxClass').change(function() {
  if($(this).is(':checked')) {
    declarationsError.hide(); // hide error message
  }
});

旁注:您生成复选框的循环应为

for (int i = 0; i < Model.DeclarationQuestions.Count; i++)
{
  @Html.CheckBoxFor(m => m.DeclarationQuestions[i].Answer, new { id = "DeclarationQuestions" + i})
}