在表单通过初始客户端验证之后返回的服务器端验证错误的元素触发错误的最佳方法是什么?
$("#contact_form").validate({ submitHandler: function(form) { $.ajax({ type: 'POST',dataType: 'json',url: '/contact/send',data: $(form).serialize(),success: function(response) { if(response.error) { //server came back with validation issues var fields = response.fields; for(var i=0,var len = fields.length; i < len; i++) { var field_name = fields[i].name; var field_error = fields[i].error; // TRIGGER ERROR ON AFFECTED ELEMENT } return false; } //everything went ok,so let's show a thanks message showThanks(); } } });
我在想:
$(form).find("[name='" + field_name + "']").triggerError(field_error);
解决方法
我想我从
Validator/showErrors的文件中得出结论
var validator = $("#contact_form").validate(); validator.showErrors({"state": "Bad state."});