MVC对复选框的不显眼的验证不起作用 [英] MVC unobtrusive validation on checkbox not working
问题描述
我正在尝试实现 这篇文章.换句话说,我正在尝试对条款和条件复选框实施不显眼的验证.如果用户未选中复选框,则输入应标记为无效.
I'm trying to implement the code as mentioned in this post. In other words I'm trying to implement unobtrusive validation on a terms and conditions checkbox. If the user hasn't selected the checkbox, then the input should be marked as invalid.
这是服务器端验证器代码,我已添加:
This is the server side Validator code, I've added:
/// <summary>
/// Validation attribute that demands that a boolean value must be true.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public class MustBeTrueAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
return value != null && value is bool && (bool)value;
}
}
这是模型
[MustBeTrue(ErrorMessage = "You must accept the terms and conditions")]
[DisplayName("Accept terms and conditions")]
public bool AcceptsTerms { get; set; }
这是我的观点:
@Html.EditorFor(x => x.AcceptTermsAndConditions)
@Html.LabelFor(x => x.AcceptTermsAndConditions)
@Html.ValidationMessageFor(x => x.AcceptTermsAndConditions)
这是我用来附加验证器客户端的 jQuery:
and this is the jQuery I've used to attach the validator client side:
$.validator.unobtrusive.adapters.addBool("mustbetrue", "required");
然而,客户端脚本似乎没有启动.每当我按下提交按钮时,对其他字段的验证就会正常进行,但对条款和条件似乎没有启动.这是我点击提交按钮后代码在 Firebug 中的样子.
The client side script doesn't appear to be kicking in, however. Whenever I press the submit button, validation on the other fields kicks in fine, but the validation for the Terms & conditions doesn't seem to kick in. This is how the code looks in Firebug after I've clicked the submit button.
<input type="checkbox" value="true" name="AcceptTermsAndConditions" id="AcceptTermsAndConditions" data-val-required="The I confirm that I am authorised to join this website and I accept the terms and conditions field is required." data-val="true" class="check-box">
<input type="hidden" value="false" name="AcceptTermsAndConditions">
<label for="AcceptTermsAndConditions">I confirm that I am authorised to join this website and I accept the terms and conditions</label>
<span data-valmsg-replace="true" data-valmsg-for="AcceptTermsAndConditions" class="field-validation-valid"></span>
有什么想法吗?我错过了一步吗?这让我上厕所!
Any ideas? Have I missed out a step? This is driving me potty!
提前致谢
推荐答案
Sniffer,
除了实现Darin的方案,还需要修改jquery.validate.unobtrusive.js
文件.在这个文件中,你必须添加一个mustbetrue"验证方法,如下:
In addition to implementing Darin's solution, you also need to modify the file jquery.validate.unobtrusive.js
. In this file, you must add a "mustbetrue" validation method, as follows:
$jQval.addMethod("mustbetrue", function (value, element, param) {
// check if dependency is met
if (!this.depend(param, element))
return "dependency-mismatch";
return element.checked;
});
然后(我一开始忘记加了),你还必须在jquery.validate.unobtrusive.js
中添加以下内容:
Then (I forgot to add this at first), you must also add the following to jquery.validate.unobtrusive.js
:
adapters.add("mustbetrue", function (options) {
setValidationValues(options, "mustbetrue", true);
});
咨询师
这篇关于MVC对复选框的不显眼的验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!