Problem with custom jQuery.validator rule
I'm having a problem with this code that I can't seem to figure out:
$.validator.addMethod('street_address', function (value, element) {
var address = value + ', ' + $(element).parent().parent().find("#venue_city_id option:selected").text(),
geocoder = new google.maps.Geocoder(),
that = this;
geocoder.geocode({address: address}, function (results, status) {
return that.optional(element) || status == google.maps.GeocoderStatus.OK;
});
}, 'invalid address');
It works to invalidate an incorrect address (one that is not geocodeable by Google). The problem is even when it returns true, I'm still getting 'invalid address'. Any ideas on what the problem 开发者_高级运维might be?
You return from inside callback in
geocoder.geocode({address: address}, function (results, status) {
return that.optional(element) || status == google.maps.GeocoderStatus.OK;
});
Not from validator function.
Moreover, geocoder.geocode
performs, I believe, asynchronous ajax query, so you'll not be able to tell whether your location is "geocodeable" when leaving you validator. There should be a specific validator for such cases in jQuery validation, called remote
or something, which validates basing on the result of ajax query.
精彩评论