bootstrapValidator.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /**
  2. * BootstrapValidator (https://github.com/nghuuphuoc/bootstrapvalidator)
  3. *
  4. * A jQuery plugin to validate form fields. Use with Bootstrap 3
  5. *
  6. * @author http://twitter.com/nghuuphuoc
  7. * @copyright (c) 2013 - 2014 Nguyen Huu Phuoc
  8. * @license MIT
  9. */
  10. (function($) {
  11. var BootstrapValidator = function(form, options) {
  12. this.$form = $(form);
  13. this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
  14. this.$invalidField = null; // First invalid field
  15. this.$submitButton = null; // The submit button which is clicked to submit form
  16. // Validating status
  17. this.STATUS_NOT_VALIDATED = 'NOT_VALIDATED';
  18. this.STATUS_VALIDATING = 'VALIDATING';
  19. this.STATUS_INVALID = 'INVALID';
  20. this.STATUS_VALID = 'VALID';
  21. this._init();
  22. };
  23. // The default options
  24. BootstrapValidator.DEFAULT_OPTIONS = {
  25. // The form CSS class
  26. elementClass: 'bootstrap-validator-form',
  27. // Default invalid message
  28. message: 'This value is not valid',
  29. // Shows ok/error/loading icons based on the field validity.
  30. // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
  31. // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
  32. // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
  33. //
  34. // Examples:
  35. // - Use Glyphicons icons:
  36. // feedbackIcons: {
  37. // valid: 'glyphicon glyphicon-ok',
  38. // invalid: 'glyphicon glyphicon-remove',
  39. // validating: 'glyphicon glyphicon-refresh'
  40. // }
  41. // - Use FontAwesome icons:
  42. // feedbackIcons: {
  43. // valid: 'fa fa-check',
  44. // invalid: 'fa fa-times',
  45. // validating: 'fa fa-refresh'
  46. // }
  47. feedbackIcons: {
  48. valid: null,
  49. invalid: null,
  50. validating: null
  51. },
  52. // The submit buttons selector
  53. // These buttons will be disabled to prevent the valid form from multiple submissions
  54. submitButtons: 'button[type="submit"]',
  55. // The custom submit handler
  56. // It will prevent the form from the default submission
  57. //
  58. // submitHandler: function(validator, form) {
  59. // - validator is the BootstrapValidator instance
  60. // - form is the jQuery object present the current form
  61. // }
  62. submitHandler: null,
  63. // Live validating option
  64. // Can be one of 3 values:
  65. // - enabled: The plugin validates fields as soon as they are changed
  66. // - disabled: Disable the live validating. The error messages are only shown after the form is submitted
  67. // - submitted: The live validating is enabled after the form is submitted
  68. live: 'enabled',
  69. // Map the field name with validator rules
  70. fields: null
  71. };
  72. BootstrapValidator.prototype = {
  73. constructor: BootstrapValidator,
  74. /**
  75. * Init form
  76. */
  77. _init: function() {
  78. var that = this,
  79. options = {
  80. trigger: this.$form.attr('data-bv-trigger'),
  81. message: this.$form.attr('data-bv-message'),
  82. submitButtons: this.$form.attr('data-bv-submitbuttons'),
  83. live: this.$form.attr('data-bv-live'),
  84. fields: {},
  85. feedbackIcons: {
  86. valid: this.$form.attr('data-bv-feedbackicons-valid'),
  87. invalid: this.$form.attr('data-bv-feedbackicons-invalid'),
  88. validating: this.$form.attr('data-bv-feedbackicons-validating')
  89. }
  90. },
  91. validator,
  92. v, // Validator name
  93. enabled,
  94. optionName,
  95. optionValue,
  96. html5AttrName,
  97. html5Attrs;
  98. this.$form
  99. // Disable client side validation in HTML 5
  100. .attr('novalidate', 'novalidate')
  101. .addClass(this.options.elementClass)
  102. // Disable the default submission first
  103. .on('submit.bv', function(e) {
  104. e.preventDefault();
  105. that.validate();
  106. })
  107. .on('click', this.options.submitButtons, function() {
  108. that.$submitButton = $(this);
  109. })
  110. // Find all fields which have either "name" or "data-bv-field" attribute
  111. .find('[name], [data-bv-field]').each(function() {
  112. var $field = $(this);
  113. // Don't initialize hidden input
  114. if ('hidden' == $field.attr('type')) {
  115. return;
  116. }
  117. var field = $field.attr('name') || $field.attr('data-bv-field');
  118. $field.attr('data-bv-field', field);
  119. options.fields[field] = $.extend({}, {
  120. trigger: $field.attr('data-bv-trigger'),
  121. message: $field.attr('data-bv-message'),
  122. container: $field.attr('data-bv-container'),
  123. selector: $field.attr('data-bv-selector'),
  124. validators: {}
  125. }, options.fields[field]);
  126. for (v in $.fn.bootstrapValidator.validators) {
  127. validator = $.fn.bootstrapValidator.validators[v];
  128. enabled = $field.attr('data-bv-' + v.toLowerCase()) + '';
  129. html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
  130. if ((html5Attrs && enabled != 'false')
  131. || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
  132. {
  133. // Try to parse the options via attributes
  134. validator.html5Attributes = validator.html5Attributes || { message: 'message' };
  135. options.fields[field]['validators'][v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, options.fields[field]['validators'][v]);
  136. for (html5AttrName in validator.html5Attributes) {
  137. optionName = validator.html5Attributes[html5AttrName];
  138. optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
  139. if (optionValue) {
  140. if ('true' == optionValue) {
  141. optionValue = true;
  142. } else if ('false' == optionValue) {
  143. optionValue = false;
  144. }
  145. options.fields[field]['validators'][v][optionName] = optionValue;
  146. }
  147. }
  148. }
  149. }
  150. });
  151. this.options = $.extend(true, this.options, options);
  152. for (var field in this.options.fields) {
  153. this._initField(field);
  154. }
  155. this.setLiveMode(this.options.live);
  156. },
  157. /**
  158. * Init field
  159. *
  160. * @param {String} field The field name
  161. */
  162. _initField: function(field) {
  163. if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
  164. return;
  165. }
  166. var fields = this.getFieldElements(field);
  167. // We don't need to validate non-existing fields
  168. if (fields == null) {
  169. delete this.options.fields[field];
  170. return;
  171. }
  172. for (var validatorName in this.options.fields[field].validators) {
  173. if (!$.fn.bootstrapValidator.validators[validatorName]) {
  174. delete this.options.fields[field].validators[validatorName];
  175. }
  176. }
  177. var that = this,
  178. type = fields.attr('type'),
  179. event = ('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup',
  180. total = fields.length,
  181. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type);
  182. for (var i = 0; i < total; i++) {
  183. var $field = $(fields[i]),
  184. $parent = $field.parents('.form-group'),
  185. // Allow user to indicate where the error messages are shown
  186. $message = this.options.fields[field].container ? $parent.find(this.options.fields[field].container) : this._getMessageContainer($field);
  187. // Set the attribute to indicate the fields which are defined by selector
  188. if (!$field.attr('data-bv-field')) {
  189. $field.attr('data-bv-field', field);
  190. }
  191. // Whenever the user change the field value, mark it as not validated yet
  192. $field.on(event + '.update.bv', function() {
  193. updateAll ? that.updateStatus(field, that.STATUS_NOT_VALIDATED, null)
  194. : that.updateElementStatus($(this), that.STATUS_NOT_VALIDATED, null);
  195. });
  196. // Create help block elements for showing the error messages
  197. $field.data('bv.messages', $message);
  198. for (validatorName in this.options.fields[field].validators) {
  199. $field.data('bv.result.' + validatorName, this.STATUS_NOT_VALIDATED);
  200. if (!updateAll || i == total - 1) {
  201. $('<small/>')
  202. .css('display', 'none')
  203. .attr('data-bv-validator', validatorName)
  204. .html(this.options.fields[field].validators[validatorName].message || this.options.fields[field].message || this.options.message)
  205. .addClass('help-block')
  206. .appendTo($message);
  207. }
  208. }
  209. // Prepare the feedback icons
  210. // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
  211. if (this.options.feedbackIcons
  212. && this.options.feedbackIcons.validating && this.options.feedbackIcons.invalid && this.options.feedbackIcons.valid
  213. && (!updateAll || i == total - 1))
  214. {
  215. $parent.addClass('has-feedback');
  216. var $icon = $('<i/>').css('display', 'none').addClass('form-control-feedback').attr('data-bv-field', field).insertAfter($field);
  217. // The feedback icon does not render correctly if there is no label
  218. // https://github.com/twbs/bootstrap/issues/12873
  219. if ($parent.find('label').length == 0) {
  220. $icon.css('top', 0);
  221. }
  222. }
  223. }
  224. if (this.options.fields[field]['enabled'] == null) {
  225. this.options.fields[field]['enabled'] = true;
  226. }
  227. },
  228. /**
  229. * Get the element to place the error messages
  230. *
  231. * @param {jQuery} $field The field element
  232. * @returns {jQuery}
  233. */
  234. _getMessageContainer: function($field) {
  235. var $parent = $field.parent();
  236. if ($parent.hasClass('form-group')) {
  237. return $parent;
  238. }
  239. var cssClasses = $parent.attr('class');
  240. if (!cssClasses) {
  241. return this._getMessageContainer($parent);
  242. }
  243. cssClasses = cssClasses.split(' ');
  244. var n = cssClasses.length;
  245. for (var i = 0; i < n; i++) {
  246. if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i]) || /^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
  247. return $parent;
  248. }
  249. }
  250. return this._getMessageContainer($parent);
  251. },
  252. /**
  253. * Called when all validations are completed
  254. */
  255. _submit: function() {
  256. if (!this.isValid()) {
  257. if ('submitted' == this.options.live) {
  258. this.setLiveMode('enabled');
  259. }
  260. // Focus to the first invalid field
  261. if (this.$invalidField) {
  262. this.$invalidField.focus();
  263. }
  264. return;
  265. }
  266. // Call the custom submission if enabled
  267. if (this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
  268. // Turn off the submit handler, so user can call form.submit() inside their submitHandler method
  269. this.$form.off('submit.bv');
  270. this.options.submitHandler.call(this, this, this.$form, this.$submitButton);
  271. } else {
  272. this.disableSubmitButtons(true);
  273. // Submit form
  274. this.$form.off('submit.bv').submit();
  275. }
  276. },
  277. // --- Public methods ---
  278. /**
  279. * Retrieve the field elements by given name
  280. *
  281. * @param {String} field The field name
  282. * @returns {null|jQuery[]}
  283. */
  284. getFieldElements: function(field) {
  285. var fields = this.$form.find(this.options.fields[field].selector || '[name="' + field + '"]');
  286. return (fields.length == 0) ? null : fields;
  287. },
  288. /**
  289. * Set live validating mode
  290. *
  291. * @param {String} mode Live validating mode. Can be 'enabled', 'disabled', 'submitted'
  292. * @returns {BootstrapValidator}
  293. */
  294. setLiveMode: function(mode) {
  295. this.options.live = mode;
  296. if ('submitted' == mode) {
  297. return this;
  298. }
  299. var that = this;
  300. for (var field in this.options.fields) {
  301. (function(f) {
  302. var fields = that.getFieldElements(f);
  303. if (fields) {
  304. var type = fields.attr('type'),
  305. total = fields.length,
  306. updateAll = (total == 1) || ('radio' == type) || ('checkbox' == type),
  307. trigger = that.options.fields[field].trigger
  308. || that.options.trigger
  309. || (('radio' == type || 'checkbox' == type || 'file' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup'),
  310. events = trigger.split(' ').map(function(item) {
  311. return item + '.live.bv';
  312. }).join(' ');
  313. for (var i = 0; i < total; i++) {
  314. ('enabled' == mode)
  315. ? $(fields[i]).on(events, function() {
  316. updateAll ? that.validateField(f) : that.validateFieldElement($(this), false);
  317. })
  318. : $(fields[i]).off(events);
  319. }
  320. }
  321. })(field);
  322. }
  323. return this;
  324. },
  325. /**
  326. * Disable/enable submit buttons
  327. *
  328. * @param {Boolean} disabled Can be true or false
  329. * @returns {BootstrapValidator}
  330. */
  331. disableSubmitButtons: function(disabled) {
  332. if (!disabled) {
  333. this.$form.find(this.options.submitButtons).removeAttr('disabled');
  334. } else if (this.options.live != 'disabled') {
  335. // Don't disable if the live validating mode is disabled
  336. this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
  337. }
  338. return this;
  339. },
  340. /**
  341. * Validate the form
  342. *
  343. * @return {BootstrapValidator}
  344. */
  345. validate: function() {
  346. if (!this.options.fields) {
  347. return this;
  348. }
  349. this.disableSubmitButtons(true);
  350. for (var field in this.options.fields) {
  351. this.validateField(field);
  352. }
  353. this._submit();
  354. return this;
  355. },
  356. /**
  357. * Validate given field
  358. *
  359. * @param {String} field The field element
  360. * @returns {BootstrapValidator}
  361. */
  362. validateField: function(field) {
  363. var fields = this.getFieldElements(field),
  364. type = fields.attr('type'),
  365. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  366. for (var i = 0; i < n; i++) {
  367. this.validateFieldElement($(fields[i]), (n == 1));
  368. }
  369. return this;
  370. },
  371. /**
  372. * Validate field element
  373. *
  374. * @param {jQuery} $field The field element
  375. * @param {Boolean} updateAll If true, update status of all elements which have the same name
  376. * @returns {BootstrapValidator}
  377. */
  378. validateFieldElement: function($field, updateAll) {
  379. var that = this,
  380. field = $field.attr('data-bv-field'),
  381. validators = this.options.fields[field].validators,
  382. validatorName,
  383. validateResult;
  384. // We don't need to validate disabled, hidden field
  385. if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
  386. return this;
  387. }
  388. for (validatorName in validators) {
  389. if ($field.data('bv.dfs.' + validatorName)) {
  390. $field.data('bv.dfs.' + validatorName).reject();
  391. }
  392. // Don't validate field if it is already done
  393. var result = $field.data('bv.result.' + validatorName);
  394. if (result == this.STATUS_VALID || result == this.STATUS_INVALID) {
  395. continue;
  396. }
  397. $field.data('bv.result.' + validatorName, this.STATUS_VALIDATING);
  398. validateResult = $.fn.bootstrapValidator.validators[validatorName].validate(this, $field, validators[validatorName]);
  399. if ('object' == typeof validateResult) {
  400. updateAll ? this.updateStatus(field, this.STATUS_VALIDATING, validatorName)
  401. : this.updateElementStatus($field, this.STATUS_VALIDATING, validatorName);
  402. $field.data('bv.dfs.' + validatorName, validateResult);
  403. validateResult.done(function($f, v, isValid) {
  404. // v is validator name
  405. $f.removeData('bv.dfs.' + v);
  406. updateAll ? that.updateStatus($f.attr('data-bv-field'), isValid ? that.STATUS_VALID : that.STATUS_INVALID, v)
  407. : that.updateElementStatus($f, isValid ? that.STATUS_VALID : that.STATUS_INVALID, v);
  408. if (isValid && 'disabled' == that.options.live) {
  409. that._submit();
  410. }
  411. });
  412. } else if ('boolean' == typeof validateResult) {
  413. updateAll ? this.updateStatus(field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName)
  414. : this.updateElementStatus($field, validateResult ? this.STATUS_VALID : this.STATUS_INVALID, validatorName);
  415. }
  416. }
  417. return this;
  418. },
  419. /**
  420. * Update all validating results of elements which have the same field name
  421. *
  422. * @param {String} field The field name
  423. * @param {String} status The status
  424. * Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  425. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  426. * @return {BootstrapValidator}
  427. */
  428. updateStatus: function(field, status, validatorName) {
  429. var fields = this.getFieldElements(field),
  430. type = fields.attr('type'),
  431. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  432. for (var i = 0; i < n; i++) {
  433. this.updateElementStatus($(fields[i]), status, validatorName);
  434. }
  435. return this;
  436. },
  437. /**
  438. * Update validating result of given element
  439. *
  440. * @param {String} field The field name
  441. * @param {String} status The status
  442. * Can be 'NOT_VALIDATED', 'VALIDATING', 'INVALID' or 'VALID'
  443. * @param {String} [validatorName] The validator name. If null, the method updates validity result for all validators
  444. * @return {BootstrapValidator}
  445. */
  446. updateElementStatus: function($field, status, validatorName) {
  447. var that = this,
  448. field = $field.attr('data-bv-field'),
  449. $parent = $field.parents('.form-group'),
  450. $message = $field.data('bv.messages'),
  451. $rowErrors = $parent.find('.help-block[data-bv-validator]'),
  452. $errors = $message.find('.help-block[data-bv-validator]'),
  453. $icon = $parent.find('.form-control-feedback[data-bv-field="' + field + '"]');
  454. // Update status
  455. if (validatorName) {
  456. $field.data('bv.result.' + validatorName, status);
  457. } else {
  458. for (var v in this.options.fields[field].validators) {
  459. $field.data('bv.result.' + v, status);
  460. }
  461. }
  462. // Show/hide error elements and feedback icons
  463. switch (status) {
  464. case this.STATUS_VALIDATING:
  465. this.disableSubmitButtons(true);
  466. $parent.removeClass('has-success').removeClass('has-error');
  467. // TODO: Show validating message
  468. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  469. if ($icon) {
  470. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
  471. }
  472. break;
  473. case this.STATUS_INVALID:
  474. this.disableSubmitButtons(true);
  475. $parent.removeClass('has-success').addClass('has-error');
  476. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').show() : $errors.show();
  477. if ($icon) {
  478. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
  479. }
  480. break;
  481. case this.STATUS_VALID:
  482. validatorName ? $errors.filter('[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  483. // If the field is valid (passes all validators)
  484. var validField = ($errors.filter(function() {
  485. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  486. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  487. }).length == 0);
  488. this.disableSubmitButtons(validField ? false : true);
  489. if ($icon) {
  490. $icon
  491. .removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid)
  492. .addClass(validField ? this.options.feedbackIcons.valid : this.options.feedbackIcons.invalid)
  493. .show();
  494. }
  495. // Check if all fields in the same row are valid
  496. var validRow = ($rowErrors.filter(function() {
  497. var display = $(this).css('display'), v = $(this).attr('data-bv-validator');
  498. return ('block' == display) || ($field.data('bv.result.' + v) != that.STATUS_VALID);
  499. }).length == 0);
  500. $parent.removeClass('has-error has-success').addClass(validRow ? 'has-success' : 'has-error');
  501. break;
  502. case this.STATUS_NOT_VALIDATED:
  503. default:
  504. this.disableSubmitButtons(false);
  505. $parent.removeClass('has-success').removeClass('has-error');
  506. validatorName ? $errors.filter('.help-block[data-bv-validator="' + validatorName + '"]').hide() : $errors.hide();
  507. if ($icon) {
  508. $icon.removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
  509. }
  510. break;
  511. }
  512. return this;
  513. },
  514. /**
  515. * Check the form validity
  516. *
  517. * @returns {Boolean}
  518. */
  519. isValid: function() {
  520. var fields, field, $field,
  521. type, status, validatorName,
  522. n, i;
  523. for (field in this.options.fields) {
  524. if (this.options.fields[field] == null || !this.options.fields[field]['enabled']) {
  525. continue;
  526. }
  527. fields = this.getFieldElements(field);
  528. type = fields.attr('type');
  529. n = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
  530. for (i = 0; i < n; i++) {
  531. $field = $(fields[i]);
  532. if ($field.is(':disabled') || $field.is(':hidden') || !$field.is(':visible')) {
  533. continue;
  534. }
  535. for (validatorName in this.options.fields[field].validators) {
  536. status = $field.data('bv.result.' + validatorName);
  537. if (status == this.STATUS_NOT_VALIDATED || status == this.STATUS_VALIDATING) {
  538. return false;
  539. }
  540. if (status == this.STATUS_INVALID) {
  541. this.$invalidField = $field;
  542. return false;
  543. }
  544. }
  545. }
  546. }
  547. return true;
  548. },
  549. // Useful APIs which aren't used internally
  550. /**
  551. * Reset the form
  552. *
  553. * @param {Boolean} resetFormData Reset current form data
  554. * @return {BootstrapValidator}
  555. */
  556. resetForm: function(resetFormData) {
  557. var field, fields, total, type, validator;
  558. for (field in this.options.fields) {
  559. fields = this.getFieldElements(field);
  560. total = fields.length;
  561. for (var i = 0; i < total; i++) {
  562. for (validator in this.options.fields[field].validators) {
  563. $(fields[i]).removeData('bv.dfs.' + validator);
  564. }
  565. }
  566. // Mark field as not validated yet
  567. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  568. if (resetFormData) {
  569. type = fields.attr('type');
  570. ('radio' == type || 'checkbox' == type) ? fields.removeAttr('checked').removeAttr('selected') : fields.val('');
  571. }
  572. }
  573. this.$invalidField = null;
  574. this.$submitButton = null;
  575. // Enable submit buttons
  576. this.disableSubmitButtons(false);
  577. return this;
  578. },
  579. /**
  580. * Enable/Disable all validators to given field
  581. *
  582. * @param {String} field The field name
  583. * @param {Boolean} enabled Enable/Disable field validators
  584. * @return {BootstrapValidator}
  585. */
  586. enableFieldValidators: function(field, enabled) {
  587. this.options.fields[field]['enabled'] = enabled;
  588. this.updateStatus(field, this.STATUS_NOT_VALIDATED, null);
  589. return this;
  590. }
  591. };
  592. // Plugin definition
  593. $.fn.bootstrapValidator = function(options) {
  594. return this.each(function() {
  595. var $this = $(this), data = $this.data('bootstrapValidator');
  596. if (!data) {
  597. $this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
  598. }
  599. if ('string' == typeof options) {
  600. data[options]();
  601. }
  602. });
  603. };
  604. // Available validators
  605. $.fn.bootstrapValidator.validators = {};
  606. $.fn.bootstrapValidator.Constructor = BootstrapValidator;
  607. }(window.jQuery));