jQuery : XMLHttp request : why use ‘async: false’ in client side validations

Yes async: false is now deprecated according to jQuery documentation. However, you many need to use sync to make custom validator for gems like ClientSideValidation.

ClientSideValidations.validators.remote['zipcode'] = function(element, options) {
  if ($.ajax({
    url: '/validators/zipcode',
    data: { id: element.val() },
    // async *must* be false
    async: false
  }).status == 404) { return options.message; }

All we’re doing here is checking to see if the resource exists (in this case the given zipcode) and if it doesn’t the error message is returned.

Notice that the remote call is forced to async: false. This is necessary and the validator may not work properly if this is left out.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s