jQuery validator plugin is really useful in case of form validation. Almost all possible rules can be added for the form validation. There are cases where we need to do the validation that involves server. In those cases the jQuery validator offers remote method.
The documentation for remote method.
Now the form I was trying to validate had couple issues regarding remote method
But the remote method documentation recommended to use dataFilter for validation. Using other ajax method made the jQuery validator abnormal and was behaving without a pattern. As soon as I removed those non recommended methods and used dataFilter inside remote method it worked. Following is the final working verion of remote method:
The documentation for remote method.
Now the form I was trying to validate had couple issues regarding remote method
- It was not able to handle internal server error (500) from server response
- Even after validation passed it was not able to submit the form
Internal server error (500) handling
jQuery validator does not handle the internal server error (500) response from server. So in case of remote method usuage if we have the control over the ajax call, then the server should always return 200 even in case of internal server error (500) and have a stauts fail in the json response.
On the clinet end using that status we can easily show the error message.
The form was not submitted for remote method
I added some of the standard ajax method with the jQuery validator remote method. Those methods are:
complete:function(xhr, textStatus){ if (textStatus == 'error') validator.showErrors(dataObj.error); }
But the remote method documentation recommended to use dataFilter for validation. Using other ajax method made the jQuery validator abnormal and was behaving without a pattern. As soon as I removed those non recommended methods and used dataFilter inside remote method it worked. Following is the final working verion of remote method:
remoteValidationHandler: function (REMOTE_URL, dataObj) { return { url: REMOTE_URL, dataType: "json", data: dataObj.ajaxdata, dataFilter: function (response) { var data = JSON.parse(response); if (!data.error) { if (data.info) $(dataObj.target).val(data.info); return '"true"'; } return "\"" + dataObj.error + "\""; } } }
Related to jQuery validator
Uncaught TypeError: Cannot read property 'settings' of undefined jquery validation
Comments
Post a Comment