bootstrap4.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * FormValidation (http://formvalidation.io)
  3. * The best jQuery plugin to validate form fields. Support Bootstrap, Foundation, Pure, SemanticUI, UIKit and custom frameworks
  4. *
  5. * @author https://twitter.com/formvalidation
  6. * @copyright (c) 2013 - 2016 Nguyen Huu Phuoc
  7. * @license http://formvalidation.io/license/
  8. */
  9. /**
  10. * This class supports validating Bootstrap 4 alpha form (http://v4-alpha.getbootstrap.com/)
  11. */
  12. (function($) {
  13. FormValidation.Framework.Bootstrap4 = function(element, options, namespace) {
  14. options = $.extend(true, {
  15. button: {
  16. selector: '[type="submit"]:not([formnovalidate])',
  17. // The class of disabled button
  18. disabled: 'disabled'
  19. },
  20. err: {
  21. // http://v4-alpha.getbootstrap.com/components/forms/#validation
  22. clazz: 'form-control-feedback',
  23. parent: '^(.*)(col|offset)-(xs|sm|md|lg)-[0-9]+(.*)$'
  24. },
  25. // - Use FontAwesome icons:
  26. // icon: {
  27. // valid: 'fa fa-check',
  28. // invalid: 'fa fa-times',
  29. // validating: 'fa fa-refresh',
  30. // feedback: 'form-control-feedback'
  31. // }
  32. icon: {
  33. valid: null,
  34. invalid: null,
  35. validating: null,
  36. feedback: 'fv-control-feedback'
  37. },
  38. row: {
  39. // By default, each field is placed inside the <div class="form-group"></div>
  40. // http://v4-alpha.getbootstrap.com/components/forms/#form-groups
  41. selector: '.form-group',
  42. valid: 'has-success',
  43. invalid: 'has-warning',
  44. feedback: 'fv-has-feedback'
  45. }
  46. }, options);
  47. FormValidation.Base.apply(this, [element, options, namespace]);
  48. };
  49. FormValidation.Framework.Bootstrap4.prototype = $.extend({}, FormValidation.Base.prototype, {
  50. /**
  51. * Specific framework might need to adjust the icon position
  52. *
  53. * @param {jQuery} $field The field element
  54. * @param {jQuery} $icon The icon element
  55. */
  56. _fixIcon: function($field, $icon) {
  57. var ns = this._namespace,
  58. type = $field.attr('type'),
  59. field = $field.attr('data-' + ns + '-field'),
  60. row = this.options.fields[field].row || this.options.row.selector,
  61. $parent = $field.closest(row);
  62. // Place it after the container of checkbox/radio
  63. // so when clicking the icon, it doesn't effect to the checkbox/radio element
  64. if ('checkbox' === type || 'radio' === type) {
  65. var $fieldParent = $field.parent();
  66. if ($fieldParent.hasClass('form-check')) {
  67. $icon.insertAfter($fieldParent);
  68. } else if ($fieldParent.parent().hasClass('form-check')) {
  69. $icon.insertAfter($fieldParent.parent());
  70. }
  71. }
  72. // Fix feedback icons in input-group
  73. if ($parent.find('.input-group').length !== 0) {
  74. $icon.addClass('fv-bootstrap-icon-input-group')
  75. .insertAfter($parent.find('.input-group').eq(0));
  76. }
  77. },
  78. /**
  79. * Create a tooltip or popover
  80. * It will be shown when focusing on the field
  81. *
  82. * @param {jQuery} $field The field element
  83. * @param {String} message The message
  84. * @param {String} type Can be 'tooltip' or 'popover'
  85. */
  86. _createTooltip: function($field, message, type) {
  87. var ns = this._namespace,
  88. $icon = $field.data(ns + '.icon');
  89. if ($icon) {
  90. switch (type) {
  91. case 'popover':
  92. $icon
  93. .css({
  94. 'cursor': 'pointer',
  95. 'pointer-events': 'auto'
  96. })
  97. .popover('destroy')
  98. .popover({
  99. container: 'body',
  100. content: message,
  101. html: true,
  102. placement: 'top',
  103. trigger: 'hover click'
  104. });
  105. break;
  106. case 'tooltip':
  107. /* falls through */
  108. default:
  109. $icon
  110. .css({
  111. 'cursor': 'pointer',
  112. 'pointer-events': 'auto'
  113. })
  114. .tooltip('dispose')
  115. .tooltip({
  116. container: 'body',
  117. html: true,
  118. placement: 'top',
  119. title: message
  120. });
  121. break;
  122. }
  123. }
  124. },
  125. /**
  126. * Destroy the tooltip or popover
  127. *
  128. * @param {jQuery} $field The field element
  129. * @param {String} type Can be 'tooltip' or 'popover'
  130. */
  131. _destroyTooltip: function($field, type) {
  132. var ns = this._namespace,
  133. $icon = $field.data(ns + '.icon');
  134. if ($icon) {
  135. switch (type) {
  136. case 'popover':
  137. $icon
  138. .css({
  139. 'cursor': '',
  140. 'pointer-events': 'none'
  141. })
  142. .popover('destroy');
  143. break;
  144. case 'tooltip':
  145. /* falls through */
  146. default:
  147. $icon
  148. .css({
  149. 'cursor': '',
  150. 'pointer-events': 'none'
  151. })
  152. .tooltip('dispose');
  153. break;
  154. }
  155. }
  156. },
  157. /**
  158. * Hide a tooltip or popover
  159. *
  160. * @param {jQuery} $field The field element
  161. * @param {String} type Can be 'tooltip' or 'popover'
  162. */
  163. _hideTooltip: function($field, type) {
  164. var ns = this._namespace,
  165. $icon = $field.data(ns + '.icon');
  166. if ($icon) {
  167. switch (type) {
  168. case 'popover':
  169. $icon.popover('hide');
  170. break;
  171. case 'tooltip':
  172. /* falls through */
  173. default:
  174. $icon.tooltip('hide');
  175. break;
  176. }
  177. }
  178. },
  179. /**
  180. * Show a tooltip or popover
  181. *
  182. * @param {jQuery} $field The field element
  183. * @param {String} type Can be 'tooltip' or 'popover'
  184. */
  185. _showTooltip: function($field, type) {
  186. var ns = this._namespace,
  187. $icon = $field.data(ns + '.icon');
  188. if ($icon) {
  189. switch (type) {
  190. case 'popover':
  191. $icon.popover('show');
  192. break;
  193. case 'tooltip':
  194. /* falls through */
  195. default:
  196. $icon.tooltip('show');
  197. break;
  198. }
  199. }
  200. }
  201. });
  202. }(jQuery));