event.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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('a@b@c@example.com');
  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('just"not"right@example.com');
  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('this is"not\\allowed@example.com');
  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('Abc.example.com');
  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('A@b@c@example.com');
  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('a"b(c)d,e:f;gi[j\\k]l@example.com');
  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('just"not"right@example.com');
  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('this is"not\\allowed@example.com');
  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('A@b@c@example.com');
  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('A@b@c@example.com');
  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('just"not"right@example.com');
  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('just"not"right@example.com');
  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. var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
  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. $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
  453. $('#eventForm2').bootstrapValidator('destroy').remove();
  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. var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
  475. beforeEach(function() {
  476. $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  477. events: {
  478. formInit: 'init.form.bv',
  479. formError: 'bv.form.error',
  480. formSuccess: 'bv.form.success',
  481. fieldAdded: 'added.field.bv',
  482. fieldRemoved: 'removed.field.bv',
  483. fieldInit: 'init.field.bv',
  484. fieldError: 'bv.field.error',
  485. fieldSuccess: 'bv.field.success',
  486. fieldStatus: 'status.field.bv',
  487. validatorError: 'bv.validator.error',
  488. validatorSuccess: 'success.validator.bv'
  489. }
  490. });
  491. $([
  492. '<form class="form-horizontal" id="eventForm4">',
  493. '<div id="msg"></div>',
  494. '<div class="form-group">',
  495. '<input type="text" name="email" data-bv-emailaddress />',
  496. '</div>',
  497. '</form>'
  498. ].join('\n')).appendTo('body');
  499. $('#eventForm4')
  500. .bootstrapValidator()
  501. .on('success.field.bv', '[name="email"]', function(e, data) {
  502. $('#msg').html('triggered success.field.bv on ' + data.field);
  503. })
  504. .on('error.field.bv', '[name="email"]', function(e, data) {
  505. $('#msg').html('triggered error.field.bv on ' + data.field);
  506. })
  507. .on('bv.field.success', '[name="email"]', function(e, data) {
  508. $('#msg').html('triggered bv.field.success on ' + data.field);
  509. })
  510. .on('bv.field.error', '[name="email"]', function(e, data) {
  511. $('#msg').html('triggered bv.field.error on ' + data.field);
  512. });
  513. this.bv = $('#eventForm4').data('bootstrapValidator');
  514. this.$email = this.bv.getFieldElements('email');
  515. });
  516. afterEach(function() {
  517. $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
  518. $('#eventForm4').bootstrapValidator('destroy').remove();
  519. });
  520. it('triggers success.field.bv', function() {
  521. this.$email.val('email@domain.com');
  522. this.bv.validate();
  523. expect($('#msg').html()).not.toEqual('triggered success.field.bv on email');
  524. });
  525. it('does not trigger bv.field.success', function() {
  526. this.$email.val('email@domain.com');
  527. this.bv.validate();
  528. expect($('#msg').html()).toEqual('triggered bv.field.success on email');
  529. });
  530. it('does not trigger error.field.bv', function() {
  531. this.$email.val('Abc.example.com');
  532. this.bv.validate();
  533. expect($('#msg').html()).not.toEqual('triggered error.field.bv on email');
  534. });
  535. it('triggers bv.field.error', function() {
  536. spyOn(window, 'onerror');
  537. this.$email.val('Abc.example.com');
  538. this.bv.validate();
  539. expect($('#msg').html()).toEqual('triggered bv.field.error on email');
  540. expect(window.onerror).not.toHaveBeenCalled();
  541. });
  542. });
  543. // ---
  544. // Validator events
  545. // ---
  546. function onEmailAddressValidatorSuccess(e, data) {
  547. $('#msg').html(data.validator + ' validator passed');
  548. };
  549. function onEmailAddressValidatorError(e, data) {
  550. $('#msg').html(data.validator + ' validator did not pass');
  551. };
  552. describe('event validator declarative', function() {
  553. beforeEach(function() {
  554. $([
  555. '<form class="form-horizontal" id="eventForm">',
  556. '<div id="msg"></div>',
  557. '<div class="form-group">',
  558. '<input type="text" name="email" data-bv-emailaddress data-bv-emailaddress-onsuccess="onEmailAddressValidatorSuccess" data-bv-emailaddress-onerror="onEmailAddressValidatorError" />',
  559. '</div>',
  560. '</form>'
  561. ].join('\n')).appendTo('body');
  562. $('#eventForm').bootstrapValidator();
  563. this.bv = $('#eventForm').data('bootstrapValidator');
  564. this.$email = this.bv.getFieldElements('email');
  565. });
  566. afterEach(function() {
  567. $('#eventForm').bootstrapValidator('destroy').remove();
  568. });
  569. it('trigger data-bv-emailaddress-onsuccess', function() {
  570. this.$email.val('email@domain.com');
  571. this.bv.validate();
  572. expect($('#msg').html()).toEqual('emailAddress validator passed');
  573. });
  574. it('trigger data-bv-emailaddress-onerror', function() {
  575. this.$email.val('A@b@c@example.com');
  576. this.bv.validate();
  577. expect($('#msg').html()).toEqual('emailAddress validator did not pass');
  578. });
  579. });
  580. describe('event validator programmatically', function() {
  581. beforeEach(function() {
  582. $([
  583. '<form class="form-horizontal" id="eventForm">',
  584. '<div id="msg"></div>',
  585. '<div class="form-group">',
  586. '<input type="text" name="email" data-bv-emailaddress />',
  587. '</div>',
  588. '</form>'
  589. ].join('\n')).appendTo('body');
  590. $('#eventForm').bootstrapValidator({
  591. fields: {
  592. email: {
  593. validators: {
  594. emailAddress: {
  595. onSuccess: function(e, data) {
  596. $('#msg').html('emailAddress validator: onSuccess() called');
  597. },
  598. onError: function(e, data) {
  599. $('#msg').html('emailAddress validator: onError() called');
  600. },
  601. message: 'The email address is not valid'
  602. }
  603. }
  604. }
  605. }
  606. });
  607. this.bv = $('#eventForm').data('bootstrapValidator');
  608. this.$email = this.bv.getFieldElements('email');
  609. });
  610. afterEach(function() {
  611. $('#eventForm').bootstrapValidator('destroy').remove();
  612. });
  613. it('call onSuccess()', function() {
  614. this.$email.val('email@domain.com');
  615. this.bv.validate();
  616. expect($('#msg').html()).toEqual('emailAddress validator: onSuccess() called');
  617. });
  618. it('call onError()', function() {
  619. this.$email.val('A@b@c@example.com');
  620. this.bv.validate();
  621. expect($('#msg').html()).toEqual('emailAddress validator: onError() called');
  622. });
  623. });