.VALIDATE:取消基于远程响应的字段验证 [英] .VALIDATE : CANCEL THE VALIDATION FOR A FIELD BASED ON REMOTE RESPONSE
问题描述
考虑以下代码:
$( document ).ready(function() {
$("#register_employee_form").validate({
errorElement: "p",
rules:
{
forename: "required",
surname: "required",
reportingto:
{
required : true,
remote: {
url: "ajax/error_checking/check_reporting_to.php",
type: "post",
data: {
reportingto: function() {
return $( "#reportingto" ).val(); // pass reportingto field
}
},
complete: function(response) {
if (response.responseText == "false")
{
// CANCEL THE VALIDATION FOR THIS FIELD
// HOW DO I DO IT??????
}
}
}
}
},
messages: {
forename: "Please enter your first name",
surname: "Please enter your last name",
reportingto: "Employee required"
},
submitHandler: function(form) {
form.submit();
}
});
});
如果AJAX调用返回的响应为false,我希望能够取消对"reportingto"字段的验证.
I want to be able to cancel the validation for the field "reportingto" if the response back from the AJAX call is false.
感谢任何想法/帮助..
Any ideas/help gratefully received..!
致谢
奥利
非常感谢,但我仍然有问题.
THANKS FOR THE RESPONSES SO FAR BUT I STILL HAVE ISSUES.
我尝试使用depends,但仍无法按要求工作:
I tried using depends but still doesn't work as required:
采用以下缩减"测试代码
Take the following "cut down" test code
test_field:
{
required: {
remote: {
param: {
url: "ajax/error_checking/check_reporting_to.php",
type: "post",
data: {
reporting_to: function() {
return $( "#test_field_to" ).val(); // pass reporting_to field
}
},
complete: function(response) {
if (response.responseText == "false")
{
cancel_flag = 1;
}
}
} // param
,
depends: function() {
if(cancel_flag == 1)
{
return false;
}
else
{
return true;
}
}
} // remote
} // required
},
和字段
<td class="column_1"><label>TEST FIELD</label></td>
<td><input name="test_field" id="test_field" value="" /></td>
如果我强制从AJAX返回"false",它仍会触发该字段的验证错误消息.
If I force a "false" return from the AJAX it still triggers a validation error message for the field.
推荐答案
使用取决于参数.
$( document ).ready(function() {
var cancelFlag=0; //Dont cancel validation
$("#register_employee_form").validate({
errorElement: "p",
rules:
{
forename:{required : true},//Do u assure on this LINE CODE BY U WORK ? I made it Proper , If ur Old code works then no problem
surname:{required : true},//Do u assure on this LINE CODE BY U WORK ? I made it Proper , If ur Old code works then no problem
reportingto:
{
required : true,
remote: {
param: {
url: "ajax/error_checking/check_reporting_to.php",
type: "post",
data: {
reportingto: function() {
return $( "#reportingto" ).val(); // pass reportingto field
}
},
complete: function(response) {
if (response.responseText == "false")
{
cancelFlag = 1;
// CANCEL THE VALIDATION FOR THIS FIELD
// HOW DO I DO IT??????
}
}
}//Param
,
depends: function() {
if(cancelFlag==1)
{
return false;
}
else
{
return true;
}
}
}//Remote
}
},
messages: {
forename: "Please enter your first name",
surname: "Please enter your last name",
reportingto: "Employee required"
},
submitHandler: function(form) {
form.submit();
}
});
});
我发现的解决方法是将标准remote:属性嵌套在名为param:的属性下.如果depends:函数返回true,它将按您期望的那样引用属性.
The workaround I found is to nest the standard remote: properties under a property named param:. If the depends: function returns true, it will reference the properties as you would expect.
有关文档,请参见 此处
For documentation see HERE
上面的代码应该起作用,U也可以选择..
The above Code should work , U have alternative also ..
reportingto:
{
required : {
remote: {
param: {
url: "ajax/error_checking/check_reporting_to.php",
type: "post",
data: {
reportingto: function() {
return $( "#reportingto" ).val(); // pass reportingto field
}
},
complete: function(response) {
if (response.responseText == "false")
{
cancelFlag = 1;
// CANCEL THE VALIDATION FOR THIS FIELD
// HOW DO I DO IT??????
}
}
}//Param
,
depends: function() {
if(cancelFlag==1)
{
return false;
}
else
{
return true;
}
}
}//Remote
}
}
FOR报告元素.
这篇关于.VALIDATE:取消基于远程响应的字段验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!