event.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
  2. TestSuite = $.extend({}, TestSuite, {
  3. Event: {
  4. onEmailValid: function(e, data) {
  5. $('#msg').html('TestSuite.Event.onEmailValid() called, ' + data.field + ' is valid');
  6. },
  7. onEmailInvalid: function(e, data) {
  8. $('#msg').html('TestSuite.Event.onEmailInvalid() called, ' + data.field + ' is invalid');
  9. },
  10. onEmailStatus: function(e, data) {
  11. $('#status').html('TestSuite.Event.onEmailStatus() called; status = ' + data.status);
  12. },
  13. onFormValid: function(e) {
  14. $('#msg').html('TestSuite.Event.onFormValid() called, form ' + $(e.target).attr('id') + ' is valid');
  15. },
  16. onFormInvalid: function(e) {
  17. $('#msg').html('TestSuite.Event.onFormInvalid() called, form ' + $(e.target).attr('id') + ' is invalid');
  18. }
  19. }
  20. });
  21. // ---
  22. // Form events
  23. // ---
  24. function onFormValid(e) {
  25. $('#msg').html('form ' + $(e.target).attr('id') + ' is valid');
  26. };
  27. function onFormInvalid(e) {
  28. $('#msg').html('form ' + $(e.target).attr('id') + ' is invalid');
  29. };
  30. describe('event form attribute callback global', function() {
  31. beforeEach(function() {
  32. $([
  33. '<form class="form-horizontal" id="eventForm" data-bv-onsuccess="onFormValid" data-bv-onerror="onFormInvalid" >',
  34. '<div id="msg"></div>',
  35. '<div class="form-group">',
  36. '<input type="text" name="email" required data-bv-emailaddress />',
  37. '</div>',
  38. '</form>'
  39. ].join('\n')).appendTo('body');
  40. $('#eventForm').bootstrapValidator();
  41. this.bv = $('#eventForm').data('bootstrapValidator');
  42. this.$email = this.bv.getFieldElements('email');
  43. });
  44. afterEach(function() {
  45. $('#eventForm').bootstrapValidator('destroy').remove();
  46. });
  47. it('call data-bv-onsuccess', function() {
  48. this.$email.val('email@domain.com');
  49. this.bv.validate();
  50. expect($('#msg').html()).toEqual('form eventForm is valid');
  51. });
  52. it('call data-bv-onerror', function() {
  53. this.$email.val('a@b@c@example.com');
  54. this.bv.validate();
  55. expect($('#msg').html()).toEqual('form eventForm is invalid');
  56. });
  57. });
  58. describe('event form attribute callback namespace', function() {
  59. beforeEach(function() {
  60. $([
  61. '<form class="form-horizontal" id="eventForm" data-bv-onsuccess="TestSuite.Event.onFormValid" data-bv-onerror="TestSuite.Event.onFormInvalid" >',
  62. '<div id="msg"></div>',
  63. '<div class="form-group">',
  64. '<input type="text" name="email" required data-bv-emailaddress />',
  65. '</div>',
  66. '</form>'
  67. ].join('\n')).appendTo('body');
  68. $('#eventForm').bootstrapValidator();
  69. this.bv = $('#eventForm').data('bootstrapValidator');
  70. this.$email = this.bv.getFieldElements('email');
  71. });
  72. afterEach(function() {
  73. $('#eventForm').bootstrapValidator('destroy').remove();
  74. });
  75. it('call data-bv-onsuccess', function() {
  76. this.$email.val('email@domain.com');
  77. this.bv.validate();
  78. expect($('#msg').html()).toEqual('TestSuite.Event.onFormValid() called, form eventForm is valid');
  79. });
  80. it('call data-bv-onerror', function() {
  81. this.$email.val('just"not"right@example.com');
  82. this.bv.validate();
  83. expect($('#msg').html()).toEqual('TestSuite.Event.onFormInvalid() called, form eventForm is invalid');
  84. });
  85. });
  86. describe('event form trigger', function() {
  87. beforeEach(function() {
  88. $([
  89. '<form class="form-horizontal" id="eventForm">',
  90. '<div id="msg"></div>',
  91. '<div class="form-group">',
  92. '<input type="text" name="email" data-bv-emailaddress />',
  93. '</div>',
  94. '</form>'
  95. ].join('\n')).appendTo('body');
  96. $('#eventForm')
  97. .bootstrapValidator()
  98. .on('success.form.bv', function(e) {
  99. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  100. })
  101. .on('error.form.bv', function(e) {
  102. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  103. });
  104. this.bv = $('#eventForm').data('bootstrapValidator');
  105. this.$email = this.bv.getFieldElements('email');
  106. });
  107. afterEach(function() {
  108. $('#eventForm').bootstrapValidator('destroy').remove();
  109. });
  110. it('trigger success.form.bv', function() {
  111. this.$email.val('email@domain.com');
  112. this.bv.validate();
  113. expect($('#msg').html()).toEqual('form eventForm triggered success.form.bv event');
  114. });
  115. it('trigger error.form.bv', function() {
  116. this.$email.val('this is"not\\allowed@example.com');
  117. this.bv.validate();
  118. expect($('#msg').html()).toEqual('form eventForm triggered error.form.bv event');
  119. });
  120. });
  121. describe('event form programmatically', function() {
  122. beforeEach(function() {
  123. $([
  124. '<form class="form-horizontal" id="eventForm">',
  125. '<div id="msg"></div>',
  126. '<div class="form-group">',
  127. '<input type="text" name="email" data-bv-emailaddress />',
  128. '</div>',
  129. '</form>'
  130. ].join('\n')).appendTo('body');
  131. $('#eventForm').bootstrapValidator({
  132. onSuccess: function(e) {
  133. $('#msg').html('onSuccess() called');
  134. },
  135. onError: function(e) {
  136. $('#msg').html('onError() called');
  137. }
  138. });
  139. this.bv = $('#eventForm').data('bootstrapValidator');
  140. this.$email = this.bv.getFieldElements('email');
  141. });
  142. afterEach(function() {
  143. $('#eventForm').bootstrapValidator('destroy').remove();
  144. });
  145. it('call onSuccess()', function() {
  146. this.$email.val('email@domain.com');
  147. this.bv.validate();
  148. expect($('#msg').html()).toEqual('onSuccess() called');
  149. });
  150. it('call onError()', function() {
  151. this.$email.val('Abc.example.com');
  152. this.bv.validate();
  153. expect($('#msg').html()).toEqual('onError() called');
  154. });
  155. });
  156. // ---
  157. // Field events
  158. // ---
  159. function onEmailValid(e, data) {
  160. $('#msg').html(data.field + ' is valid');
  161. };
  162. function onEmailInvalid(e, data) {
  163. $('#msg').html(data.field + ' is invalid');
  164. };
  165. function onEmailStatus(e, data) {
  166. $('#status').html(data.status);
  167. };
  168. describe('event field attribute callback global', function() {
  169. beforeEach(function() {
  170. $([
  171. '<form class="form-horizontal" id="eventForm">',
  172. '<div id="msg"></div>',
  173. '<div id="status"></div>',
  174. '<div class="form-group">',
  175. '<input type="text" name="email" data-bv-emailaddress data-bv-onsuccess="onEmailValid" data-bv-onerror="onEmailInvalid" data-bv-onstatus="onEmailStatus" />',
  176. '</div>',
  177. '</form>'
  178. ].join('\n')).appendTo('body');
  179. $('#eventForm').bootstrapValidator();
  180. this.bv = $('#eventForm').data('bootstrapValidator');
  181. this.$email = this.bv.getFieldElements('email');
  182. });
  183. afterEach(function() {
  184. $('#eventForm').bootstrapValidator('destroy').remove();
  185. });
  186. it('call data-bv-onsuccess', function() {
  187. this.$email.val('email@domain.com');
  188. this.bv.validate();
  189. expect($('#msg').html()).toEqual('email is valid');
  190. expect($('#status').html()).toEqual(this.bv.STATUS_VALID);
  191. });
  192. it('call data-bv-onerror', function() {
  193. this.$email.val('A@b@c@example.com');
  194. this.bv.validate();
  195. expect($('#msg').html()).toEqual('email is invalid');
  196. expect($('#status').html()).toEqual(this.bv.STATUS_INVALID);
  197. });
  198. });
  199. describe('event field attribute callback namespace', function() {
  200. beforeEach(function() {
  201. $([
  202. '<form class="form-horizontal" id="eventForm">',
  203. '<div id="msg"></div>',
  204. '<div id="status"></div>',
  205. '<div class="form-group">',
  206. '<input type="text" name="email" data-bv-emailaddress data-bv-onsuccess="TestSuite.Event.onEmailValid" data-bv-onerror="TestSuite.Event.onEmailInvalid" data-bv-onstatus="TestSuite.Event.onEmailStatus" />',
  207. '</div>',
  208. '</form>'
  209. ].join('\n')).appendTo('body');
  210. $('#eventForm').bootstrapValidator();
  211. this.bv = $('#eventForm').data('bootstrapValidator');
  212. this.$email = this.bv.getFieldElements('email');
  213. });
  214. afterEach(function() {
  215. $('#eventForm').bootstrapValidator('destroy').remove();
  216. });
  217. it('call data-bv-onsuccess', function() {
  218. this.$email.val('email@domain.com');
  219. this.bv.validate();
  220. expect($('#msg').html()).toEqual('TestSuite.Event.onEmailValid() called, email is valid');
  221. expect($('#status').html()).toEqual('TestSuite.Event.onEmailStatus() called; status = ' + this.bv.STATUS_VALID);
  222. });
  223. it('call data-bv-onerror', function() {
  224. this.$email.val('a"b(c)d,e:f;gi[j\\k]l@example.com');
  225. this.bv.validate();
  226. expect($('#msg').html()).toEqual('TestSuite.Event.onEmailInvalid() called, email is invalid');
  227. expect($('#status').html()).toEqual('TestSuite.Event.onEmailStatus() called; status = ' + this.bv.STATUS_INVALID);
  228. });
  229. });
  230. describe('event field trigger', function() {
  231. beforeEach(function() {
  232. $([
  233. '<form class="form-horizontal" id="eventForm">',
  234. '<div id="msg"></div>',
  235. '<div class="form-group">',
  236. '<input type="text" name="email" data-bv-emailaddress />',
  237. '</div>',
  238. '</form>'
  239. ].join('\n')).appendTo('body');
  240. $('#eventForm')
  241. .bootstrapValidator()
  242. .on('success.field.bv', '[name="email"]', function(e, data) {
  243. $('#msg').html('triggered success.field.bv on ' + data.field);
  244. })
  245. .on('error.field.bv', '[name="email"]', function(e, data) {
  246. $('#msg').html('triggered error.field.bv on ' + data.field);
  247. });
  248. this.bv = $('#eventForm').data('bootstrapValidator');
  249. this.$email = this.bv.getFieldElements('email');
  250. });
  251. afterEach(function() {
  252. $('#eventForm').bootstrapValidator('destroy').remove();
  253. });
  254. it('trigger success.field.bv', function() {
  255. this.$email.val('email@domain.com');
  256. this.bv.validate();
  257. expect($('#msg').html()).toEqual('triggered success.field.bv on email');
  258. });
  259. it('trigger error.field.bv', function() {
  260. this.$email.val('just"not"right@example.com');
  261. this.bv.validate();
  262. expect($('#msg').html()).toEqual('triggered error.field.bv on email');
  263. });
  264. });
  265. describe('event field programmatically', function() {
  266. beforeEach(function() {
  267. $([
  268. '<form class="form-horizontal" id="eventForm">',
  269. '<div id="msg"></div>',
  270. '<div class="form-group">',
  271. '<input type="text" name="email" data-bv-emailaddress />',
  272. '</div>',
  273. '</form>'
  274. ].join('\n')).appendTo('body');
  275. $('#eventForm').bootstrapValidator({
  276. fields: {
  277. email: {
  278. onSuccess: function(e, data) {
  279. $('#msg').html('onSuccess() called');
  280. },
  281. onError: function(e, data) {
  282. $('#msg').html('onError() called');
  283. }
  284. }
  285. }
  286. });
  287. this.bv = $('#eventForm').data('bootstrapValidator');
  288. this.$email = this.bv.getFieldElements('email');
  289. });
  290. afterEach(function() {
  291. $('#eventForm').bootstrapValidator('destroy').remove();
  292. });
  293. it('call onSuccess()', function() {
  294. this.$email.val('email@domain.com');
  295. this.bv.validate();
  296. expect($('#msg').html()).toEqual('onSuccess() called');
  297. });
  298. it('call onError()', function() {
  299. this.$email.val('this is"not\\allowed@example.com');
  300. this.bv.validate();
  301. expect($('#msg').html()).toEqual('onError() called');
  302. });
  303. });
  304. // ---
  305. // Modifying default events
  306. // ---
  307. describe('event form trigger with default events', function() {
  308. beforeEach(function() {
  309. $([
  310. '<form class="form-horizontal" id="eventForm1">',
  311. '<div id="msg"></div>',
  312. '<div class="form-group">',
  313. '<input type="text" name="email" data-bv-emailaddress />',
  314. '</div>',
  315. '</form>'
  316. ].join('\n')).appendTo('body');
  317. $('#eventForm1')
  318. .bootstrapValidator()
  319. .on('bv.form.success', function(e) {
  320. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.success event');
  321. })
  322. .on('success.form.bv', function(e) {
  323. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  324. })
  325. .on('bv.form.error', function(e) {
  326. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.error event');
  327. })
  328. .on('error.form.bv', function(e) {
  329. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  330. });
  331. this.bv = $('#eventForm1').data('bootstrapValidator');
  332. this.$email = this.bv.getFieldElements('email');
  333. });
  334. afterEach(function() {
  335. $('#eventForm1').bootstrapValidator('destroy').remove();
  336. });
  337. it('does not trigger bv.form.success', function() {
  338. this.$email.val('email@domain.com');
  339. this.bv.validate();
  340. expect($('#msg').html()).not.toEqual('form eventForm1 triggered bv.form.success event');
  341. });
  342. it('triggers success.form.bv', function() {
  343. this.$email.val('email@domain.com');
  344. this.bv.validate();
  345. expect($('#msg').html()).toEqual('form eventForm1 triggered success.form.bv event');
  346. });
  347. it('does not trigger bv.form.error', function() {
  348. this.$email.val('A@b@c@example.com');
  349. this.bv.validate();
  350. expect($('#msg').html()).not.toEqual('form eventForm1 triggered bv.form.error event');
  351. });
  352. it('triggers error.form.bv', function() {
  353. this.$email.val('A@b@c@example.com');
  354. this.bv.validate();
  355. expect($('#msg').html()).toEqual('form eventForm1 triggered error.form.bv event');
  356. });
  357. });
  358. describe('event field trigger with default events', function() {
  359. beforeEach(function() {
  360. $([
  361. '<form class="form-horizontal" id="eventForm3">',
  362. '<div id="msg"></div>',
  363. '<div class="form-group">',
  364. '<input type="text" name="email" data-bv-emailaddress />',
  365. '</div>',
  366. '</form>'
  367. ].join('\n')).appendTo('body');
  368. $('#eventForm3')
  369. .bootstrapValidator()
  370. .on('success.field.bv', '[name="email"]', function(e, data) {
  371. $('#msg').html('triggered success.field.bv on ' + data.field);
  372. })
  373. .on('error.field.bv', '[name="email"]', function(e, data) {
  374. $('#msg').html('triggered error.field.bv on ' + data.field);
  375. })
  376. .on('bv.field.success', '[name="email"]', function(e, data) {
  377. $('#msg').html('triggered bv.field.success on ' + data.field);
  378. })
  379. .on('bv.field.error', '[name="email"]', function(e, data) {
  380. $('#msg').html('triggered bv.field.error on ' + data.field);
  381. });
  382. this.bv = $('#eventForm3').data('bootstrapValidator');
  383. this.$email = this.bv.getFieldElements('email');
  384. });
  385. afterEach(function() {
  386. $('#eventForm3').bootstrapValidator('destroy').remove();
  387. });
  388. it('triggers success.field.bv', function() {
  389. this.$email.val('email@domain.com');
  390. this.bv.validate();
  391. expect($('#msg').html()).toEqual('triggered success.field.bv on email');
  392. });
  393. it('does not trigger bv.field.success', function() {
  394. this.$email.val('email@domain.com');
  395. this.bv.validate();
  396. expect($('#msg').html()).not.toEqual('triggered bv.field.success on email');
  397. });
  398. it('does not trigger error.field.bv', function() {
  399. this.$email.val('just"not"right@example.com');
  400. this.bv.validate();
  401. expect($('#msg').html()).toEqual('triggered error.field.bv on email');
  402. });
  403. it('triggers bv.field.error', function() {
  404. this.$email.val('just"not"right@example.com');
  405. this.bv.validate();
  406. expect($('#msg').html()).not.toEqual('triggered bv.field.error on email');
  407. });
  408. });
  409. describe('event form trigger with events changed', function() {
  410. beforeEach(function() {
  411. $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  412. events: {
  413. formInit: 'init.form.bv',
  414. formError: 'bv.form.error',
  415. formSuccess: 'bv.form.success',
  416. fieldAdded: 'added.field.bv',
  417. fieldRemoved: 'removed.field.bv',
  418. fieldInit: 'init.field.bv',
  419. fieldError: 'bv.field.error',
  420. fieldSuccess: 'bv.field.success',
  421. fieldStatus: 'status.field.bv',
  422. validatorError: 'bv.validator.error',
  423. validatorSuccess: 'success.validator.bv'
  424. }
  425. });
  426. $([
  427. '<form class="form-horizontal" id="eventForm2">',
  428. '<div id="msg"></div>',
  429. '<div class="form-group">',
  430. '<input type="text" name="email" data-bv-emailaddress />',
  431. '</div>',
  432. '</form>'
  433. ].join('\n')).appendTo('body');
  434. $('#eventForm2')
  435. .bootstrapValidator()
  436. .on('bv.form.success', function(e) {
  437. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.success event');
  438. })
  439. .on('success.form.bv', function(e) {
  440. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  441. })
  442. .on('bv.form.error', function(e) {
  443. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.error event');
  444. })
  445. .on('error.form.bv', function(e) {
  446. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  447. });
  448. this.bv = $('#eventForm2').data('bootstrapValidator');
  449. this.$email = this.bv.getFieldElements('email');
  450. });
  451. afterEach(function() {
  452. $('#eventForm2').bootstrapValidator('destroy').remove();
  453. $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
  454. });
  455. it('triggers bv.form.success', function() {
  456. this.$email.val('email@domain.com');
  457. this.bv.validate();
  458. expect($('#msg').html()).toEqual('form eventForm2 triggered bv.form.success event');
  459. });
  460. it('does not trigger success.form.bv', function() {
  461. this.$email.val('email@domain.com');
  462. this.bv.validate();
  463. expect($('#msg').html()).not.toEqual('form eventForm2 triggered success.form.bv event');
  464. });
  465. it('triggers bv.form.error', function() {
  466. spyOn(window, 'onerror');
  467. this.$email.val('this is"not\\allowed@example.com');
  468. this.bv.validate();
  469. expect($('#msg').html()).toEqual('form eventForm2 triggered bv.form.error event');
  470. expect(window.onerror).not.toHaveBeenCalled();
  471. });
  472. });
  473. describe('event field trigger with events changed', function() {
  474. beforeEach(function() {
  475. $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  476. events: {
  477. formInit: 'init.form.bv',
  478. formError: 'bv.form.error',
  479. formSuccess: 'bv.form.success',
  480. fieldAdded: 'added.field.bv',
  481. fieldRemoved: 'removed.field.bv',
  482. fieldInit: 'init.field.bv',
  483. fieldError: 'bv.field.error',
  484. fieldSuccess: 'bv.field.success',
  485. fieldStatus: 'status.field.bv',
  486. validatorError: 'bv.validator.error',
  487. validatorSuccess: 'success.validator.bv'
  488. }
  489. });
  490. $([
  491. '<form class="form-horizontal" id="eventForm4">',
  492. '<div id="msg"></div>',
  493. '<div class="form-group">',
  494. '<input type="text" name="email" data-bv-emailaddress />',
  495. '</div>',
  496. '</form>'
  497. ].join('\n')).appendTo('body');
  498. $('#eventForm4')
  499. .bootstrapValidator()
  500. .on('success.field.bv', '[name="email"]', function(e, data) {
  501. $('#msg').html('triggered success.field.bv on ' + data.field);
  502. })
  503. .on('error.field.bv', '[name="email"]', function(e, data) {
  504. $('#msg').html('triggered error.field.bv on ' + data.field);
  505. })
  506. .on('bv.field.success', '[name="email"]', function(e, data) {
  507. $('#msg').html('triggered bv.field.success on ' + data.field);
  508. })
  509. .on('bv.field.error', '[name="email"]', function(e, data) {
  510. $('#msg').html('triggered bv.field.error on ' + data.field);
  511. });
  512. this.bv = $('#eventForm4').data('bootstrapValidator');
  513. this.$email = this.bv.getFieldElements('email');
  514. });
  515. afterEach(function() {
  516. $('#eventForm4').bootstrapValidator('destroy').remove();
  517. $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
  518. });
  519. it('triggers success.field.bv', function() {
  520. this.$email.val('email@domain.com');
  521. this.bv.validate();
  522. expect($('#msg').html()).not.toEqual('triggered success.field.bv on email');
  523. });
  524. it('does not trigger bv.field.success', function() {
  525. this.$email.val('email@domain.com');
  526. this.bv.validate();
  527. expect($('#msg').html()).toEqual('triggered bv.field.success on email');
  528. });
  529. it('does not trigger error.field.bv', function() {
  530. this.$email.val('Abc.example.com');
  531. this.bv.validate();
  532. expect($('#msg').html()).not.toEqual('triggered error.field.bv on email');
  533. });
  534. it('triggers bv.field.error', function() {
  535. spyOn(window, 'onerror');
  536. this.$email.val('Abc.example.com');
  537. this.bv.validate();
  538. expect($('#msg').html()).toEqual('triggered bv.field.error on email');
  539. expect(window.onerror).not.toHaveBeenCalled();
  540. });
  541. });
  542. // ---
  543. // Validator events
  544. // ---
  545. function onEmailAddressValidatorSuccess(e, data) {
  546. $('#msg').html(data.validator + ' validator passed');
  547. };
  548. function onEmailAddressValidatorError(e, data) {
  549. $('#msg').html(data.validator + ' validator did not pass');
  550. };
  551. describe('event validator declarative', function() {
  552. beforeEach(function() {
  553. $([
  554. '<form class="form-horizontal" id="eventForm">',
  555. '<div id="msg"></div>',
  556. '<div class="form-group">',
  557. '<input type="text" name="email" data-bv-emailaddress data-bv-emailaddress-onsuccess="onEmailAddressValidatorSuccess" data-bv-emailaddress-onerror="onEmailAddressValidatorError" />',
  558. '</div>',
  559. '</form>'
  560. ].join('\n')).appendTo('body');
  561. $('#eventForm').bootstrapValidator();
  562. this.bv = $('#eventForm').data('bootstrapValidator');
  563. this.$email = this.bv.getFieldElements('email');
  564. });
  565. afterEach(function() {
  566. $('#eventForm').bootstrapValidator('destroy').remove();
  567. });
  568. it('trigger data-bv-emailaddress-onsuccess', function() {
  569. this.$email.val('email@domain.com');
  570. this.bv.validate();
  571. expect($('#msg').html()).toEqual('emailAddress validator passed');
  572. });
  573. it('trigger data-bv-emailaddress-onerror', function() {
  574. this.$email.val('A@b@c@example.com');
  575. this.bv.validate();
  576. expect($('#msg').html()).toEqual('emailAddress validator did not pass');
  577. });
  578. });
  579. describe('event validator programmatically', function() {
  580. beforeEach(function() {
  581. $([
  582. '<form class="form-horizontal" id="eventForm">',
  583. '<div id="msg"></div>',
  584. '<div class="form-group">',
  585. '<input type="text" name="email" data-bv-emailaddress />',
  586. '</div>',
  587. '</form>'
  588. ].join('\n')).appendTo('body');
  589. $('#eventForm').bootstrapValidator({
  590. fields: {
  591. email: {
  592. validators: {
  593. emailAddress: {
  594. onSuccess: function(e, data) {
  595. $('#msg').html('emailAddress validator: onSuccess() called');
  596. },
  597. onError: function(e, data) {
  598. $('#msg').html('emailAddress validator: onError() called');
  599. },
  600. message: 'The email address is not valid'
  601. }
  602. }
  603. }
  604. }
  605. });
  606. this.bv = $('#eventForm').data('bootstrapValidator');
  607. this.$email = this.bv.getFieldElements('email');
  608. });
  609. afterEach(function() {
  610. $('#eventForm').bootstrapValidator('destroy').remove();
  611. });
  612. it('call onSuccess()', function() {
  613. this.$email.val('email@domain.com');
  614. this.bv.validate();
  615. expect($('#msg').html()).toEqual('emailAddress validator: onSuccess() called');
  616. });
  617. it('call onError()', function() {
  618. this.$email.val('A@b@c@example.com');
  619. this.bv.validate();
  620. expect($('#msg').html()).toEqual('emailAddress validator: onError() called');
  621. });
  622. });