[英]Error jquery validation plugin depends. Form valid when fields are not valid
I have a web form wizard for which I need validation that supports (a) hidden fields and (b) conditional validation. 我有一个Web表单向导,我需要对其进行验证,该向导支持(a)隐藏字段和(b)条件验证。 Jquery seems the best fit. jQuery似乎最合适。 Once all is set up as follows ( simplified example ): 一切设置如下( 简化示例 ):
$( "form[name=form]" ).validate({
...
ignore: [],
rules: {
'form[owner]': {
required: true,
},
'form[bank_account]': {
required: true,
depends: function(element){
return $('#my_select').is(':checked');`
messages : {
'form[owner]' : {
required: "The owner is necessary",
},
'form[bank_account]' : {
required: "This field must be filled out when switch on",
},
...
})
On javascript console, I got the following error: 在javascript控制台上,出现以下错误:
Cannot read property 'call' of undefined. 无法读取未定义的属性“ call”。 Exception occurred when checking element form[bank_account], check the method depends 检查元素表单[bank_account]时发生异常,检查方法取决于
All fields should be validated accordingly to form validation each and every time the form is to be submitted 每次提交表单时,都应相应地验证所有字段以进行表单验证
How can this be fixed? 如何解决?
As I did not find any questions similar to mine around. 由于我没有发现任何类似的问题。 The solution I found is the following: 我找到的解决方案如下:
Instead of using the depends
method, use a function to establish if the field should or should not be required, including it in the conditions on which this operation should rely (ie if a checkbox is checked or not) 可以使用函数来确定是否需要该字段,而不是使用depends
方法,包括在此操作应依赖的条件下(例如,是否选中复选框)
$( "form[name=form]" ).validate({
...
ignore: [],
rules: {
'form[owner]': {
required: true,
},
'form[bank_account]': {
required: function(element){
return $('#my_select').is(':checked') && $('form_bank_account').val();
messages : {
'form[owner]' : {
required: "The owner is necessary",
},
'form[bank_account]' : {
required: "This field must be filled out when switch on",
},
...
})
Either documentation is confusing, or the library has a bug. 文档令人困惑,或者库有错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.