2011-05-31 50 views
3

我实现一个自定义的验证器来检查是否有至少东西检查:ASP.NET MVC 3.0验证MVCContrib的CheckBoxList

public class AtLestOneRequiredAttribute : RequiredAttribute 
    { 
     public override bool IsValid(object value) 
     { 
      return (value != null); 
     } 
    } 

    public class RequiredListValidator : DataAnnotationsModelValidator<AtLestOneRequiredAttribute> 
    { 
     private readonly string errorMessage; 

     /// <summary> 
     /// Initializes a new instance of the <see cref="EmailValidator"/> class. 
     /// </summary> 
     /// <param name="metadata">The metadata.</param> 
     /// <param name="context">The context.</param> 
     /// <param name="attribute">The attribute.</param> 
     public RequiredListValidator(ModelMetadata metadata, ControllerContext context, AtLestOneRequiredAttribute attribute) 
      : base(metadata, context, attribute) 
     { 
      this.errorMessage = attribute.ErrorMessage; 
     } 

     /// <summary> 
     /// Retrieves a collection of client validation rules. 
     /// </summary> 
     /// <returns>A collection of client validation rules.</returns> 
     public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() 
     { 
      var rule = new ModelClientValidationRule 
      { 
       ErrorMessage = errorMessage, 
       ValidationType = "atlestonerequired" 
      }; 

      return new[] { rule }; 
     }   
    } 

我也产生不引人注目的验证特性对于选择(http://weblogs.asp.net/srkirkland/archive/2011/03/08/adding-unobtrusive-validation-to-mvccontrib-fluent-html.aspx

型号:

[AtLestOneRequired(ErrorMessage="At least one selection required")] 
     public IList<int> MyCheckBox{ get; set; } 

查看:

@this.CheckBoxList(x => x.MyCheckBox).Options(Model.MyCheckBoxes).IncludeUnobtrusiveValidationAttributes(Html) 

在结果我得到这个网站在不显眼的属性:

<div data-val="true" data-val-atlestonerequired="At least one selection required" id="MyCheckBox"> 
    <input id="MyCheckBox_0" name="MyCheckBox" type="checkbox" value="1"/> 
    <label for="MyCheckBox_0" id="MyCheckBox_0_Label">A</label> 
    <input id="MyCheckBox_1" name="MyCheckBox" type="checkbox" value="2"/> 
    <label for="MyCheckBox_1" id="MyCheckBox_1_Label">B</label> 
    <input id="MyCheckBox_2" name="MyCheckBox" type="checkbox" value="3"/> 
    <label for="MyCheckBox_2" id="MyCheckBox_2_Label">C</label> 
    <input id="MyCheckBox_3" name="MyCheckBox" type="checkbox" value="4"/> 
    <label for="MyCheckBox_3" id="MyCheckBox_3_Label">D</label> 
    .... 
</div> 

但我validaton不会对客户端的工作,什么是我需要做的,使其工作?我想可能是我需要实现自定义jQuery validaton方法在这种情况下,如果默认情况下它不工作?

想补充一点:

jQuery.validator.addMethod("atlestonerequired", function (value, element) { 
      return (value != null); 
     }); 

     jQuery.validator.unobtrusive.adapters.addBool('atlestonerequired'); 

不起作用。

+0

我想知道答案,就一个? :( – Maidot 2011-06-01 01:26:22

+0

我也在寻找相同的解决方案! – imdadhusen 2011-10-18 13:47:36

回答

0

我不知道这个问题的可接受的解决方案。但这是一个解决它的方法。

我有复选框为:

<input name="TermsNConditions" type="checkbox" id="TermsNConditions" tabindex="12" /> 

该模型具有这个属性:

[Required(ErrorMessage = "(Please accept the terms and conditions)")] 
    [DefaultValue(0)] 
    [RegularExpression(@"[^0]", ErrorMessage = "(Please accept the terms and conditions)")] 
    public int AcceptTermsNConditions { get; set; } 

和一个隐藏字段为它

<%:Html.HiddenFor(model=>model.AcceptTermsNConditions) %> 

我调用JavaScript更改事件复选框如下:

$(function() { 
     $("#TermsNConditions").live("change", function() { 
      if ($("#TermsNConditions").attr('checked')) { 
       $("#AcceptTermsNConditions").val(1); 
      } 
      else { 
       $("#AcceptTermsNConditions").val(0); 
      } 
     }); 
     if ($("#AcceptTermsNConditions").val() == 1) { 
      $("#TermsNConditions").attr('checked', true); 
     } 
    }); 

上述脚本设置隐藏字段的值,并根据该隐藏字段验证模型。

1

在这个问题上花了一些时间。 客户端验证不起作用的原因是不引人注意的验证选择需要验证的项目的方式。特别是以下行:

$(selector).find(":input[data-val=true]").each(function() { 
    $jQval.unobtrusive.parseElement(this, true); 
}); 

如果您发现该“:输入”只适用于input|select|textarea|button

这意味着你的"div[data-val=true]"不会被选中。

解决此问题的方法是执行类似于单选按钮的必需。

<div name="CheckBox" id="CheckBox"> 
    <input type="checkbox" data-val-cbrequired="Required" data-val="true" value="Default" name="CheckBox_0" id="CheckBox_0"> 
    <label for="CheckBox_0">Checkbox</label> 
    <input type="checkbox" value="Yes" name="CheckBox_1" id="CheckBox_1"> 
    <label for="CheckBox_1">Yes</label> 
    <input type="checkbox" value="No" name="CheckBox_2" id="CheckBox_2"> 
    <label for="CheckBox_2">No</label> 
</div> 

然后为jQuery验证补充:

$.validator.unobtrusive.adapters.addBool("cbrequired", "required");