MVC 3不显眼的验证 - 有条件地禁用/启用验证 [英] MVC 3 unobtrusive validation - conditionally disable/enable validation

查看:89
本文介绍了MVC 3不显眼的验证 - 有条件地禁用/启用验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有一个选项,输入尺寸的形式:

I have a form that has an option to enter dimensions for:


  • 宽度放大器;身高

  • 宽度

  • 高度

和我有两个div容器,我隐藏/显示,这取决于三个选项中选择:

And I have two container divs that I hide/show depending on which of the three options is selected:

<div class="editor-field" id="width-container">
     @Html.EditorFor(model => model.Width)
     @Html.ValidationMessageFor(model => model.Width)
</div>

<div class="editor-field" id="height-container">
     @Html.EditorFor(model => model.Height)
     @Html.ValidationMessageFor(model => model.Height)
</div>

如果选择的高度,那么宽度没有在表格上显示,我怎么能禁止在时尚的宽输入字段不显眼的审定,让我轻松地复权如果用户改变他们的主意,即去除数据 - *属性是不是一种选择。我很高兴能创建一个CustomAttribute类来处理这个问题,但我不希望有破解标准的jQuery文件,使这项工作,因为它使更新到新版本头疼下降的轨道。如果一切都失败,我将使用添加值为0到田间地头时,他们是不可见的,然后取出显示时,他们是我惯用的伎俩。

If height is selected, then width is not displayed on the form, how can I disable the unobtrusive validation on the Width input field in a fashion that will allow me to easily re-instate it if the user changes their mind i.e. removing data-* attributes is not an option. I'm happy to create an CustomAttribute class to handle this BUT I do not want to have to hack the standard jquery files to make this work as it makes updating to new versions a headache down the track. If all else fails I'll use my usual trick of adding a value of 0 to the fields when they are not visible and then removing it when they are shown.

编辑:

请铭记,当宽度不可见它不是一个隐藏的领域本身,它只是一个输入变量,因为父div有显示器的风格,这不是对用户可见:无

Please be mindful that when Width is not visible it is not a "hidden" field per se it's just a input tag that's not visible to the user because the parent div has a style of display:none

推荐答案

您可以设置jQuery的验证程序的处理您的不显眼的验证,忽略隐藏元素:

You can set up the jQuery validator that's processing your unobtrusive validation to ignore hidden elements:

jQuery.validator.defaults.ignore = ":hidden";

// the line above is outside any $(document).ready(...) or similar
$(document).ready(function(){
    ...
});
...

这篇关于MVC 3不显眼的验证 - 有条件地禁用/启用验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆