remote.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.remote = $.extend($.fn.bootstrapValidator.i18n.remote || {}, {
  3. 'default': 'Please enter a valid value'
  4. });
  5. $.fn.bootstrapValidator.validators.remote = {
  6. html5Attributes: {
  7. message: 'message',
  8. name: 'name',
  9. type: 'type',
  10. url: 'url',
  11. data: 'data',
  12. delay: 'delay'
  13. },
  14. /**
  15. * Destroy the timer when destroying the bootstrapValidator (using validator.destroy() method)
  16. */
  17. destroy: function(validator, $field, options) {
  18. if ($field.data('bv.remote.timer')) {
  19. clearTimeout($field.data('bv.remote.timer'));
  20. $field.removeData('bv.remote.timer');
  21. }
  22. },
  23. /**
  24. * Request a remote server to check the input value
  25. *
  26. * @param {BootstrapValidator} validator Plugin instance
  27. * @param {jQuery} $field Field element
  28. * @param {Object} options Can consist of the following keys:
  29. * - url {String|Function}
  30. * - type {String} [optional] Can be GET or POST (default)
  31. * - data {Object|Function} [optional]: By default, it will take the value
  32. * {
  33. * <fieldName>: <fieldValue>
  34. * }
  35. * - delay
  36. * - name {String} [optional]: Override the field name for the request.
  37. * - message: The invalid message
  38. * - headers: Additional headers
  39. * @returns {Deferred}
  40. */
  41. validate: function(validator, $field, options) {
  42. var value = $field.val(),
  43. dfd = new $.Deferred();
  44. if (value === '') {
  45. dfd.resolve($field, 'remote', { valid: true });
  46. return dfd;
  47. }
  48. var name = $field.attr('data-bv-field'),
  49. data = options.data || {},
  50. url = options.url,
  51. type = options.type || 'GET',
  52. headers = options.headers || {};
  53. // Support dynamic data
  54. if ('function' === typeof data) {
  55. data = data.call(this, validator);
  56. }
  57. // Parse string data from HTML5 attribute
  58. if ('string' === typeof data) {
  59. data = JSON.parse(data);
  60. }
  61. // Support dynamic url
  62. if ('function' === typeof url) {
  63. url = url.call(this, validator);
  64. }
  65. data[options.name || name] = value;
  66. function runCallback() {
  67. var xhr = $.ajax({
  68. type: type,
  69. headers: headers,
  70. url: url,
  71. dataType: 'json',
  72. data: data
  73. });
  74. xhr.then(function(response) {
  75. response.valid = response.valid === true || response.valid === 'true';
  76. dfd.resolve($field, 'remote', response);
  77. });
  78. dfd.fail(function() {
  79. xhr.abort();
  80. });
  81. return dfd;
  82. }
  83. if (options.delay) {
  84. // Since the form might have multiple fields with the same name
  85. // I have to attach the timer to the field element
  86. if ($field.data('bv.remote.timer')) {
  87. clearTimeout($field.data('bv.remote.timer'));
  88. }
  89. $field.data('bv.remote.timer', setTimeout(runCallback, options.delay));
  90. return dfd;
  91. } else {
  92. return runCallback();
  93. }
  94. }
  95. };
  96. }(window.jQuery));