event.js 25 KB

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