Jquery validator with javascript remoting

720    Asked by ElveraPeasley in AWS , Asked on Jul 12, 2021

I have an input field out of several on a form, whose value needs to be validated against database values. I am using jquery validator to perform all other validations and trying to achieve database validation using visualforce remoting and validator method. What I am trying.

HTML Code

<input type="text" class="numVal" maxlength="6" "="" fdprocessedid="4nf8uh">

Validator Rule class and method snippet
$j.validator.addClassRules({ numVal: { required: true, number: true, noDecimal:true, checkDBList: true } }); $j.validator.addMethod("checkDBList",function(value) { MyController.searchDB(value, function(result, event){ if(event.status ){ if(result != null){ return true; }else{ return false; } } if(event.type == 'exception') { return false } }); }, "Please enter a valid number");

Problem My validator method is always returning false, even though a valid number is provided and the remote method is returning the correct status. I understand that visualforce remoting is asynchronous and both the functions are executing in different contexts and hence the issue. Any help to use the visualforce remoting in a correct way inside the validator method is appreciated.

Answered by Guy Rodd

I did little bit of research and closest answer I could get is this: https://stackoverflow.com/a/13203690/1343065 Only difference is instead of the Asynchronous Ajax call in the link we are triggering VisualForce remoting call which is Asynchronous in Nature (different domains, same problem). Other possible option is to make the VisualForce remoting call async=false which when skimmed through here, I din't find any option to set https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_when_to_use.htm Last option is to find $j.validator accepts remote calls and found none. I am not sure how close you are attached with jQuery validator plugin (jquery validator.addmethod), but it would be a good option to try http://verifyjs.jpillora.com/



Your Answer

Interviews

Parent Categories