禁用要求验证由JavaScript [英] Disable required validation by JavaScript
问题描述
我有一个创建表单创建一个对象。
该模型的建立具有一定的属性才可见(.hide,.show())如果一个复选框(在模型的属性)检查,被标记必需的。
不幸的是如果未选中该复选框,上隐藏的属性进行必要的验证。
我怎样才能禁用所需验证这一特性?
我试过设置输入元素虚假的数据-VAL属性,但不起作用。
一些想法?
在此先感谢
托比亚斯
更新:
下面是Java脚本code。数据-VAL属性已正确设置为false。似乎验证不关心这个属性。也有数据-VAL要求的属性,但有一个文本我不能备份。
$(函数(){
$(#MyCheckbox)
.change(函数(){
如果(this.checked){
$(#divWithChildProperties [数据-VAL])ATTR(数据-VAL,真)。
$(#divWithChildProperties).show();
}
其他{
$(#divWithChildProperties [数据-VAL])ATTR(数据-VAL,假)。
$(#divWithChildProperties).hide();
}
})
});
我已经处理这样的情况下使用自定义验证属性。而不是使用必需的属性,属性你可以做一个RequiredIf属性,并提出一些只要求你的属性,如果另一个属性是一定的价值。
下面是关于(在'一个更复杂的自定义验证部分下的页面中间的例子)这样创建属性后:<一href=\"http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2\">http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2
如果您的复选框再$ P $模型中的psents属性这应该正常工作。
如果你不想用一个新的验证来处理这个属性,你必须做一些事情不仅仅是改变数据-VAL属性设置为false。当jQuery的验证首先分析你的形式,它在表单的数据存储值。所以,简单地改变数据-VAL是不够的。你将还必须删除存储的验证数据,并重新分析表。这里有一个例子:
//做到这一点你已经删除后/修改的数据-VAL属性
$('选择为表单')removeData('unobtrusiveValidation')。
$('选择为表单')removeData('验证')。
$ .validator.unobtrusive.parse(选择你的形式');
I have a create form to create an object. The create model has some properties that are only visible (.hide, .show()) if a checkbox is checked and that are marked required (by Attribute in Model).
Unfortunatly when the checkbox is not checked, the required validation is performed on the properties hidden.
How can I disable the required validation for this properties?
I tried setting the data-val property of the input element to false but this does not work.
Some an idea?
Thanks in advance Tobias
UPDATE:
Here is the java script code. The data-val property is set correctly to false. it seems that validation does not care of this property. there is also the data-val-required attribute but there is a text i could not backup.
$(function () {
$("#MyCheckbox")
.change(function () {
if (this.checked) {
$("#divWithChildProperties [data-val]").attr("data-val", true);
$("#divWithChildProperties ").show();
}
else {
$("#divWithChildProperties [data-val]").attr("data-val", false);
$("#divWithChildProperties ").hide();
}
})
});
I've handled cases like this with a custom Validation Attribute. Instead of using the Required attribute for properties you could make a RequiredIf attribute and make some of your properties required only if another property is a certain value.
Here is a post about creating an attribute like this (the example in the middle of the page under the 'A more complex custom validator' section): http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2
If your checkbox represents a property in your model this should work fine.
If you don't want to handle this with a new validation attribute you will have to do a few more things than just change the data-val attribute to false. When jQuery validate first parses your form it stores values in the form's data. So simply changing data-val isn't enough. You will additionally have to remove the stored validation data and reparse the form. Here's an example:
// Do this after you've removed/modified the data-val attribute
$('selector for your form').removeData('unobtrusiveValidation');
$('selector for your form').removeData('validator');
$.validator.unobtrusive.parse('selector for your form');
这篇关于禁用要求验证由JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!