event.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. var originalDefaultOptions = $.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. var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
  411. beforeEach(function() {
  412. $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  413. events: {
  414. formInit: 'init.form.bv',
  415. formError: 'bv.form.error',
  416. formSuccess: 'bv.form.success',
  417. fieldAdded: 'added.field.bv',
  418. fieldRemoved: 'removed.field.bv',
  419. fieldInit: 'init.field.bv',
  420. fieldError: 'bv.field.error',
  421. fieldSuccess: 'bv.field.success',
  422. fieldStatus: 'status.field.bv',
  423. validatorError: 'bv.validator.error',
  424. validatorSuccess: 'success.validator.bv'
  425. }
  426. });
  427. $([
  428. '<form class="form-horizontal" id="eventForm2">',
  429. '<div id="msg"></div>',
  430. '<div class="form-group">',
  431. '<input type="text" name="email" data-bv-emailaddress />',
  432. '</div>',
  433. '</form>'
  434. ].join('\n')).appendTo('body');
  435. $('#eventForm2')
  436. .bootstrapValidator()
  437. .on('bv.form.success', function(e) {
  438. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.success event');
  439. })
  440. .on('success.form.bv', function(e) {
  441. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered success.form.bv event');
  442. })
  443. .on('bv.form.error', function(e) {
  444. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered bv.form.error event');
  445. })
  446. .on('error.form.bv', function(e) {
  447. $('#msg').html('form ' + $(e.target).attr('id') + ' triggered error.form.bv event');
  448. });
  449. this.bv = $('#eventForm2').data('bootstrapValidator');
  450. this.$email = this.bv.getFieldElements('email');
  451. });
  452. afterEach(function() {
  453. $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
  454. $('#eventForm2').bootstrapValidator('destroy').remove();
  455. $.fn.bootstrapValidator.DEFAULT_OPTIONS = originalDefaultOptions;
  456. });
  457. it('triggers bv.form.success', function() {
  458. this.$email.val('email@domain.com');
  459. this.bv.validate();
  460. expect($('#msg').html()).toEqual('form eventForm2 triggered bv.form.success event');
  461. });
  462. it('does not trigger success.form.bv', function() {
  463. this.$email.val('email@domain.com');
  464. this.bv.validate();
  465. expect($('#msg').html()).not.toEqual('form eventForm2 triggered success.form.bv event');
  466. });
  467. it('triggers bv.form.error', function() {
  468. spyOn(window, 'onerror');
  469. this.$email.val('this is"not\\allowed@example.com');
  470. this.bv.validate();
  471. expect($('#msg').html()).toEqual('form eventForm2 triggered bv.form.error event');
  472. expect(window.onerror).not.toHaveBeenCalled();
  473. });
  474. });
  475. describe('event field trigger with events changed', function() {
  476. var defaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;
  477. beforeEach(function() {
  478. $.fn.bootstrapValidator.DEFAULT_OPTIONS = $.extend({}, $.fn.bootstrapValidator.DEFAULT_OPTIONS, {
  479. events: {
  480. formInit: 'init.form.bv',
  481. formError: 'bv.form.error',
  482. formSuccess: 'bv.form.success',
  483. fieldAdded: 'added.field.bv',
  484. fieldRemoved: 'removed.field.bv',
  485. fieldInit: 'init.field.bv',
  486. fieldError: 'bv.field.error',
  487. fieldSuccess: 'bv.field.success',
  488. fieldStatus: 'status.field.bv',
  489. validatorError: 'bv.validator.error',
  490. validatorSuccess: 'success.validator.bv'
  491. }
  492. });
  493. $([
  494. '<form class="form-horizontal" id="eventForm4">',
  495. '<div id="msg"></div>',
  496. '<div class="form-group">',
  497. '<input type="text" name="email" data-bv-emailaddress />',
  498. '</div>',
  499. '</form>'
  500. ].join('\n')).appendTo('body');
  501. $('#eventForm4')
  502. .bootstrapValidator()
  503. .on('success.field.bv', '[name="email"]', function(e, data) {
  504. $('#msg').html('triggered success.field.bv on ' + data.field);
  505. })
  506. .on('error.field.bv', '[name="email"]', function(e, data) {
  507. $('#msg').html('triggered error.field.bv on ' + data.field);
  508. })
  509. .on('bv.field.success', '[name="email"]', function(e, data) {
  510. $('#msg').html('triggered bv.field.success on ' + data.field);
  511. })
  512. .on('bv.field.error', '[name="email"]', function(e, data) {
  513. $('#msg').html('triggered bv.field.error on ' + data.field);
  514. });
  515. this.bv = $('#eventForm4').data('bootstrapValidator');
  516. this.$email = this.bv.getFieldElements('email');
  517. });
  518. afterEach(function() {
  519. $.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
  520. $('#eventForm4').bootstrapValidator('destroy').remove();
  521. $.fn.bootstrapValidator.DEFAULT_OPTIONS = originalDefaultOptions;
  522. });
  523. it('triggers success.field.bv', function() {
  524. this.$email.val('email@domain.com');
  525. this.bv.validate();
  526. expect($('#msg').html()).not.toEqual('triggered success.field.bv on email');
  527. });
  528. it('does not trigger bv.field.success', function() {
  529. this.$email.val('email@domain.com');
  530. this.bv.validate();
  531. expect($('#msg').html()).toEqual('triggered bv.field.success on email');
  532. });
  533. it('does not trigger error.field.bv', function() {
  534. this.$email.val('Abc.example.com');
  535. this.bv.validate();
  536. expect($('#msg').html()).not.toEqual('triggered error.field.bv on email');
  537. });
  538. it('triggers bv.field.error', function() {
  539. spyOn(window, 'onerror');
  540. this.$email.val('Abc.example.com');
  541. this.bv.validate();
  542. expect($('#msg').html()).toEqual('triggered bv.field.error on email');
  543. expect(window.onerror).not.toHaveBeenCalled();
  544. });
  545. });
  546. // ---
  547. // Validator events
  548. // ---
  549. function onEmailAddressValidatorSuccess(e, data) {
  550. $('#msg').html(data.validator + ' validator passed');
  551. };
  552. function onEmailAddressValidatorError(e, data) {
  553. $('#msg').html(data.validator + ' validator did not pass');
  554. };
  555. describe('event validator declarative', function() {
  556. beforeEach(function() {
  557. $([
  558. '<form class="form-horizontal" id="eventForm">',
  559. '<div id="msg"></div>',
  560. '<div class="form-group">',
  561. '<input type="text" name="email" data-bv-emailaddress data-bv-emailaddress-onsuccess="onEmailAddressValidatorSuccess" data-bv-emailaddress-onerror="onEmailAddressValidatorError" />',
  562. '</div>',
  563. '</form>'
  564. ].join('\n')).appendTo('body');
  565. $('#eventForm').bootstrapValidator();
  566. this.bv = $('#eventForm').data('bootstrapValidator');
  567. this.$email = this.bv.getFieldElements('email');
  568. });
  569. afterEach(function() {
  570. $('#eventForm').bootstrapValidator('destroy').remove();
  571. });
  572. it('trigger data-bv-emailaddress-onsuccess', function() {
  573. this.$email.val('email@domain.com');
  574. this.bv.validate();
  575. expect($('#msg').html()).toEqual('emailAddress validator passed');
  576. });
  577. it('trigger data-bv-emailaddress-onerror', function() {
  578. this.$email.val('A@b@c@example.com');
  579. this.bv.validate();
  580. expect($('#msg').html()).toEqual('emailAddress validator did not pass');
  581. });
  582. });
  583. describe('event validator programmatically', function() {
  584. beforeEach(function() {
  585. $([
  586. '<form class="form-horizontal" id="eventForm">',
  587. '<div id="msg"></div>',
  588. '<div class="form-group">',
  589. '<input type="text" name="email" data-bv-emailaddress />',
  590. '</div>',
  591. '</form>'
  592. ].join('\n')).appendTo('body');
  593. $('#eventForm').bootstrapValidator({
  594. fields: {
  595. email: {
  596. validators: {
  597. emailAddress: {
  598. onSuccess: function(e, data) {
  599. $('#msg').html('emailAddress validator: onSuccess() called');
  600. },
  601. onError: function(e, data) {
  602. $('#msg').html('emailAddress validator: onError() called');
  603. },
  604. message: 'The email address is not valid'
  605. }
  606. }
  607. }
  608. }
  609. });
  610. this.bv = $('#eventForm').data('bootstrapValidator');
  611. this.$email = this.bv.getFieldElements('email');
  612. });
  613. afterEach(function() {
  614. $('#eventForm').bootstrapValidator('destroy').remove();
  615. });
  616. it('call onSuccess()', function() {
  617. this.$email.val('email@domain.com');
  618. this.bv.validate();
  619. expect($('#msg').html()).toEqual('emailAddress validator: onSuccess() called');
  620. });
  621. it('call onError()', function() {
  622. this.$email.val('A@b@c@example.com');
  623. this.bv.validate();
  624. expect($('#msg').html()).toEqual('emailAddress validator: onError() called');
  625. });
  626. });