bootstrapValidator.js 28 KB

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