MVC 对复选框的不引人注目的验证不起作用
我正在尝试实现 这篇文章。换句话说,我正在尝试对条款和条件复选框实施不引人注目的验证。如果用户未选中该复选框,则输入应标记为无效。
这是服务器端验证器代码,我添加了:
/// <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:
$.validator.unobtrusive.adapters.addBool("mustbetrue", "required");
客户端脚本似乎没有启动, 然而。每当我按下提交按钮时,其他字段的验证就会正常进行,但条款和条件的验证会正常进行。这就是我单击提交按钮后代码在 Firebug 中的样子。
<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>
有什么想法吗?我是不是漏掉了一步?这让我快要上厕所了!
提前致谢 S
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;
}
}
This is the model
[MustBeTrue(ErrorMessage = "You must accept the terms and conditions")]
[DisplayName("Accept terms and conditions")]
public bool AcceptsTerms { get; set; }
This is my view:
@Html.EditorFor(x => x.AcceptTermsAndConditions)
@Html.LabelFor(x => x.AcceptTermsAndConditions)
@Html.ValidationMessageFor(x => x.AcceptTermsAndConditions)
and this is the jQuery I've used to attach the validator client side:
$.validator.unobtrusive.adapters.addBool("mustbetrue", "required");
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!
Thanks in advance
S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要在自定义上实现 IClientValidatable属性,以便将您在客户端注册的
mustbetrue
适配器名称与此属性绑定:更新:
完整的工作示例。
型号:
控制器:
视图:
You need to implement IClientValidatable on your custom attribute in order to tie the
mustbetrue
adapter name that you are registering on the client side with this attribute:UPDATE:
Full working example.
Model:
Controller:
View:
Sniffer,
除了实现Darin的方案外,还需要修改
jquery.validate.unobtrusive.js
文件。在此文件中,您必须添加一个“mustbetrue”验证方法,如下所示:然后(我一开始忘记添加它了),您还必须将以下内容添加到
jquery.validate.unobtrusive.js
:辅导员本
Sniffer,
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:Then (I forgot to add this at first), you must also add the following to
jquery.validate.unobtrusive.js
:counsellorben
我不确定为什么这对我不起作用,但我选择使用您的代码并做一些稍微不同的事情。
在我的 JavaScript 加载中,我添加了以下内容,如果您选择该复选框并取消选中它,这将使该复选框触发不引人注目的验证。另外,如果您提交表格。
您还需要将 CSS 类添加到您的复选框中,如下所示。
因此,我们现在需要连接模型并放入类来处理服务器端验证(我从上面重复使用它),但稍微改变了不显眼的程度。
这是扩展 IClientValidate 的客户属性,如上例所示...
在您的模型中,对象属性设置所需的属性符号
好的,我们准备好放入 JavaScript。
是什么让这个工作有效,是......好吧。在尝试执行上面建议的答案后查看 HTML 标记后,我的值全部设置为 true,但是我选中的复选框为 false。所以,我决定让 jQuery 使用 IsChecked 来解决这个问题
I'm unsure why this didn't work for me, but I opted to use your code and do something slightly different.
On my JavaScript load I add the following, this makes the checkbox fire the unabtrusive validation if, either you select the checkbox and uncheck it. Also, if you submit the form.
You also need to add the CSS class to your checkbox, like so.
So, we now need to hook up the model and put in out class to handle the server side validation (which i'm re-using from above) but changing the unobtrusiveness slightly.
Here's the customer attribute that extends IClientValidate as in the above example...
In your model, object property set the desired attribute notations
Okay, we're ready to put in the JavaScript.
What makes this work, is... well. After looking at the HTML markup after trying to do the suggested answer above, my values were all set to true, however my checkbox checked was false. So, I decided to let jQuery work it out using IsChecked
对于那些这些解决方案都不起作用的人:
我正在使用 .Net Framework 4 和最新的 jquery 验证脚本文件来使用 Razor MVC 4。
在客户端和服务器端实现自定义属性验证后,仍然不起作用。无论如何,我的表格正在发布。
所以这里有一个问题:
JQuery 验证脚本的默认设置是忽略隐藏标签,其中隐藏是 http://api.jquery.com/ hidden-selector/,这通常不会成为问题,但我使用的 @Html.CheckBoxFor 样式是使用 CSS3 样式自定义的,该样式将显示更改为无,并显示复选框的自定义图像,所以它永远不会执行复选框上的验证规则。
我的解决方法是在自定义客户端验证规则声明之前添加这一行:
它的作用是覆盖当前页面中所有验证的忽略设置,请注意,现在它也可以对隐藏字段执行验证(副作用)。
For those that none of these solutions are working:
I'm working with Razor MVC 4 using .Net Framework 4, and the latest jquery validate script files.
After implementing the custom attribute validation in client and server side, it still doesn't work. My form is being post anyway.
So here is the catch:
JQuery validate script has a default setting of ignore hidden tags where hidden is http://api.jquery.com/hidden-selector/, that won't be a problem normally but the @Html.CheckBoxFor style that I'm using is customized with a CSS3 style that changes the display to none and a custom image of the checkbox is displayed, so it will never execute the validation rule on the checkbox.
My workaround was adding this line before the custom client validation rule declaration:
what it does is override the ignore setting for all validations in the current page, notice that now it could execute validations on hidden field too (a side effect).