jquery validation remote - validate whether data in field or not
General Description:
In my form, I have a field that requires remote validation. In the remote call, I send the value of the form element as well as the values of a couple other form elements on which this element may depend (used on the server for validation).The remote validation, when called, works great. The problem is that the remote validation is only called when there is a value in the form element. Depending on the params sent to the remote validation, this form element may be required or there may be other validation issues.
How can I force the remote validation on this field whether it has data in it or not?
Specific Scenario:
Got a roles list, and a salesperson code text input. Server side logic: (1) if salesperson role selected, salespersoncode required; (2) if salesperson role not selected, salespersoncode must be empty.Note that I do have other form element values I send to the server and there is 开发者_如何学运维other server side logic that I did not include here for brevity since they do not add value to this question and will only confuse things.
<select name="roles" id="roles" multiple="multiple" size="5"></select>
<input type="text" name="salespersoncode" id="salespersoncode" />
$("#add_user_form").validate({
rules: {
salespersoncode: {
remote: {
type: "post",
url: "/AdminJson/CheckSalespersonCode",
dataType: "json",
data: {
salespersoncode: function () { return $("#salespersoncode").val(); },
roles: function () { return $("#roles").val(); }
}
}
}
}
}
I don't want to put in the "required" validation option because the salespersoncode field is not required in all situations.
My question again: How can I force the remote validation on the salespersoncode field whether it has data in it or not?
Thanks in advance
Add this piece of code before you call the validate() method:
$.validator.methods._required = $.validator.methods.required;
$.validator.methods.required = function( value, element, param )
{
if ( $(element).is('[remote-validator]') && !$(element).hasClass('required') )
return true;
return $.validator.methods._required.call( this, value, element, param );
}
Looks like you cannot have a form element that has remote validation without also having required validation. It ends up resulting in a dependency-mismatch.
The very first line in the remote method is the check to see if it is optional - if it is optional, return a dependency-mismatch. (optional is the inverse of remote)
The solution I came up with was to use a dependency-callback for the 'required' rule. In the function for the 'required' rule, call a synchronous ajax call to test the values in the UI on the server and return true/false.
Try explicitly setting the salesPersonCode first:
salespersoncode: {
salesPersonCode: ($("#salespersoncode").val().length > 0) ? $("#salespersoncode").val() : "",
remote: {
type: "post",
url: "/AdminJson/CheckSalespersonCode",
dataType: "json",
data: {
salespersoncode: salesPersonCode; },
roles: function () { return $("#roles").val(); }
}
}
}
精彩评论