event.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. TestSuite = $.extend({}, TestSuite, {
  2. Event: {
  3. onEmailValid: function(e, data) {
  4. $('#msg').html('TestSuite.Event.onEmailValid() called, ' + data.field + ' is valid');
  5. },
  6. onEmailInvalid: function(e, data) {
  7. $('#msg').html('TestSuite.Event.onEmailInvalid() called, ' + data.field + ' is invalid');
  8. },
  9. onFormValid: function(e) {
  10. $('#msg').html('TestSuite.Event.onFormValid() called, form ' + $(e.target).attr('id') + ' is valid');
  11. },
  12. onFormInvalid: function(e) {
  13. $('#msg').html('TestSuite.Event.onFormInvalid() called, form ' + $(e.target).attr('id') + ' is invalid');
  14. }
  15. }
  16. });
  17. // ---
  18. // Form events
  19. // ---
  20. function onFormValid(e) {
  21. $('#msg').html('form ' + $(e.target).attr('id') + ' is valid');
  22. };
  23. function onFormInvalid(e) {
  24. $('#msg').html('form ' + $(e.target).attr('id') + ' is invalid');
  25. };
  26. describe('event form attribute callback global', function() {
  27. beforeEach(function() {
  28. $([
  29. '<form class="form-horizontal" id="eventForm" data-bv-onsuccess="onFormValid" data-bv-onerror="onFormInvalid" >',
  30. '<div id="msg"></div>',
  31. '<div class="form-group">',
  32. '<input type="text" name="email" required data-bv-emailaddress />',
  33. '</div>',
  34. '</form>'
  35. ].join('\n')).appendTo('body');
  36. $('#eventForm').bootstrapValidator();
  37. this.bv = $('#eventForm').data('bootstrapValidator');
  38. this.$email = this.bv.getFieldElements('email');
  39. });
  40. afterEach(function() {
  41. $('#eventForm').bootstrapValidator('destroy').remove();
  42. });
  43. it('call data-bv-onsuccess', function() {
  44. this.$email.val('email@domain.com');
  45. this.bv.validate();
  46. expect($('#msg').html()).toEqual('form eventForm is valid');
  47. });
  48. it('call data-bv-onerror', function() {
  49. this.$email.val('email@domain');
  50. this.bv.validate();
  51. expect($('#msg').html()).toEqual('form eventForm is invalid');
  52. });
  53. });
  54. describe('event form attribute callback namespace', function() {
  55. beforeEach(function() {
  56. $([
  57. '<form class="form-horizontal" id="eventForm" data-bv-onsuccess="TestSuite.Event.onFormValid" data-bv-onerror="TestSuite.Event.onFormInvalid" >',
  58. '<div id="msg"></div>',
  59. '<div class="form-group">',
  60. '<input type="text" name="email" required data-bv-emailaddress />',
  61. '</div>',
  62. '</form>'
  63. ].join('\n')).appendTo('body');
  64. $('#eventForm').bootstrapValidator();
  65. this.bv = $('#eventForm').data('bootstrapValidator');
  66. this.$email = this.bv.getFieldElements('email');
  67. });
  68. afterEach(function() {
  69. $('#eventForm').bootstrapValidator('destroy').remove();
  70. });
  71. it('call data-bv-onsuccess', function() {
  72. this.$email.val('email@domain.com');
  73. this.bv.validate();
  74. expect($('#msg').html()).toEqual('TestSuite.Event.onFormValid() called, form eventForm is valid');
  75. });
  76. it('call data-bv-onerror', function() {
  77. this.$email.val('email@domain');
  78. this.bv.validate();
  79. expect($('#msg').html()).toEqual('TestSuite.Event.onFormInvalid() called, form eventForm is invalid');
  80. });
  81. });
  82. describe('event form trigger', function() {
  83. beforeEach(function() {
  84. $([
  85. '<form class="form-horizontal" id="eventForm">',
  86. '<div id="msg"></div>',
  87. '<div class="form-group">',
  88. '<input type="text" name="email" data-bv-emailaddress />',
  89. '</div>',
  90. '</form>'
  91. ].join('\n')).appendTo('body');
  92. $('#eventForm')
  93. .bootstrapValidator()
  94. .on('success.form.bv', function(e) {
  95. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  96. })
  97. .on('error.form.bv', function(e) {
  98. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  99. });
  100. this.bv = $('#eventForm').data('bootstrapValidator');
  101. this.$email = this.bv.getFieldElements('email');
  102. });
  103. afterEach(function() {
  104. $('#eventForm').bootstrapValidator('destroy').remove();
  105. });
  106. it('trigger success.form.bv', function() {
  107. this.$email.val('email@domain.com');
  108. this.bv.validate();
  109. expect($('#msg').html()).toEqual('form eventForm triggered success.form.bv event');
  110. });
  111. it('trigger error.form.bv', function() {
  112. this.$email.val('email@domain');
  113. this.bv.validate();
  114. expect($('#msg').html()).toEqual('form eventForm triggered error.form.bv event');
  115. });
  116. });
  117. describe('event form programmatically', function() {
  118. beforeEach(function() {
  119. $([
  120. '<form class="form-horizontal" id="eventForm">',
  121. '<div id="msg"></div>',
  122. '<div class="form-group">',
  123. '<input type="text" name="email" data-bv-emailaddress />',
  124. '</div>',
  125. '</form>'
  126. ].join('\n')).appendTo('body');
  127. $('#eventForm').bootstrapValidator({
  128. onSuccess: function(e) {
  129. $('#msg').html('onSuccess() called');
  130. },
  131. onError: function(e) {
  132. $('#msg').html('onError() called');
  133. }
  134. });
  135. this.bv = $('#eventForm').data('bootstrapValidator');
  136. this.$email = this.bv.getFieldElements('email');
  137. });
  138. afterEach(function() {
  139. $('#eventForm').bootstrapValidator('destroy').remove();
  140. });
  141. it('call onSuccess()', function() {
  142. this.$email.val('email@domain.com');
  143. this.bv.validate();
  144. expect($('#msg').html()).toEqual('onSuccess() called');
  145. });
  146. it('call onError()', function() {
  147. this.$email.val('email@domain');
  148. this.bv.validate();
  149. expect($('#msg').html()).toEqual('onError() called');
  150. });
  151. });
  152. // ---
  153. // Field events
  154. // ---
  155. function onEmailValid(e, data) {
  156. $('#msg').html(data.field + ' is valid');
  157. };
  158. function onEmailInvalid(e, data) {
  159. $('#msg').html(data.field + ' is invalid');
  160. };
  161. describe('event field attribute callback global', function() {
  162. beforeEach(function() {
  163. $([
  164. '<form class="form-horizontal" id="eventForm">',
  165. '<div id="msg"></div>',
  166. '<div class="form-group">',
  167. '<input type="text" name="email" data-bv-emailaddress data-bv-onsuccess="onEmailValid" data-bv-onerror="onEmailInvalid" />',
  168. '</div>',
  169. '</form>'
  170. ].join('\n')).appendTo('body');
  171. $('#eventForm').bootstrapValidator();
  172. this.bv = $('#eventForm').data('bootstrapValidator');
  173. this.$email = this.bv.getFieldElements('email');
  174. });
  175. afterEach(function() {
  176. $('#eventForm').bootstrapValidator('destroy').remove();
  177. });
  178. it('call data-bv-onsuccess', function() {
  179. this.$email.val('email@domain.com');
  180. this.bv.validate();
  181. expect($('#msg').html()).toEqual('email is valid');
  182. });
  183. it('call data-bv-onerror', function() {
  184. this.$email.val('email@domain');
  185. this.bv.validate();
  186. expect($('#msg').html()).toEqual('email is invalid');
  187. });
  188. });
  189. describe('event field attribute callback namespace', function() {
  190. beforeEach(function() {
  191. $([
  192. '<form class="form-horizontal" id="eventForm">',
  193. '<div id="msg"></div>',
  194. '<div class="form-group">',
  195. '<input type="text" name="email" data-bv-emailaddress data-bv-onsuccess="TestSuite.Event.onEmailValid" data-bv-onerror="TestSuite.Event.onEmailInvalid" />',
  196. '</div>',
  197. '</form>'
  198. ].join('\n')).appendTo('body');
  199. $('#eventForm').bootstrapValidator();
  200. this.bv = $('#eventForm').data('bootstrapValidator');
  201. this.$email = this.bv.getFieldElements('email');
  202. });
  203. afterEach(function() {
  204. $('#eventForm').bootstrapValidator('destroy').remove();
  205. });
  206. it('call data-bv-onsuccess', function() {
  207. this.$email.val('email@domain.com');
  208. this.bv.validate();
  209. expect($('#msg').html()).toEqual('TestSuite.Event.onEmailValid() called, email is valid');
  210. });
  211. it('call data-bv-onerror', function() {
  212. this.$email.val('email@domain');
  213. this.bv.validate();
  214. expect($('#msg').html()).toEqual('TestSuite.Event.onEmailInvalid() called, email is invalid');
  215. });
  216. });
  217. describe('event field trigger', function() {
  218. beforeEach(function() {
  219. $([
  220. '<form class="form-horizontal" id="eventForm">',
  221. '<div id="msg"></div>',
  222. '<div class="form-group">',
  223. '<input type="text" name="email" data-bv-emailaddress />',
  224. '</div>',
  225. '</form>'
  226. ].join('\n')).appendTo('body');
  227. $('#eventForm')
  228. .bootstrapValidator()
  229. .on('success.field.bv', '[name="email"]', function(e, data) {
  230. $('#msg').html('triggered success.field.bv on ' + data.field);
  231. })
  232. .on('error.field.bv', '[name="email"]', function(e, data) {
  233. $('#msg').html('triggered error.field.bv on ' + data.field);
  234. });
  235. this.bv = $('#eventForm').data('bootstrapValidator');
  236. this.$email = this.bv.getFieldElements('email');
  237. });
  238. afterEach(function() {
  239. $('#eventForm').bootstrapValidator('destroy').remove();
  240. });
  241. it('trigger success.field.bv', function() {
  242. this.$email.val('email@domain.com');
  243. this.bv.validate();
  244. expect($('#msg').html()).toEqual('triggered success.field.bv on email');
  245. });
  246. it('trigger error.field.bv', function() {
  247. this.$email.val('email@domain');
  248. this.bv.validate();
  249. expect($('#msg').html()).toEqual('triggered error.field.bv on email');
  250. });
  251. });
  252. describe('event field programmatically', function() {
  253. beforeEach(function() {
  254. $([
  255. '<form class="form-horizontal" id="eventForm">',
  256. '<div id="msg"></div>',
  257. '<div class="form-group">',
  258. '<input type="text" name="email" data-bv-emailaddress />',
  259. '</div>',
  260. '</form>'
  261. ].join('\n')).appendTo('body');
  262. $('#eventForm').bootstrapValidator({
  263. fields: {
  264. email: {
  265. onSuccess: function(e, data) {
  266. $('#msg').html('onSuccess() called');
  267. },
  268. onError: function(e, data) {
  269. $('#msg').html('onError() called');
  270. },
  271. validator: {
  272. emailAddress: {}
  273. }
  274. }
  275. }
  276. });
  277. this.bv = $('#eventForm').data('bootstrapValidator');
  278. this.$email = this.bv.getFieldElements('email');
  279. });
  280. afterEach(function() {
  281. $('#eventForm').bootstrapValidator('destroy').remove();
  282. });
  283. it('call onSuccess()', function() {
  284. this.$email.val('email@domain.com');
  285. this.bv.validate();
  286. expect($('#msg').html()).toEqual('onSuccess() called');
  287. });
  288. it('call onError()', function() {
  289. this.$email.val('email@domain');
  290. this.bv.validate();
  291. expect($('#msg').html()).toEqual('onError() called');
  292. });
  293. });