event.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. // Validator events
  305. // ---
  306. function onEmailAddressValidatorSuccess(e, data) {
  307. $('#msg').html(data.validator + ' validator passed');
  308. };
  309. function onEmailAddressValidatorError(e, data) {
  310. $('#msg').html(data.validator + ' validator did not pass');
  311. };
  312. describe('event validator declarative', function() {
  313. beforeEach(function() {
  314. $([
  315. '<form class="form-horizontal" id="eventForm">',
  316. '<div id="msg"></div>',
  317. '<div class="form-group">',
  318. '<input type="text" name="email" data-bv-emailaddress data-bv-emailaddress-onsuccess="onEmailAddressValidatorSuccess" data-bv-emailaddress-onerror="onEmailAddressValidatorError" />',
  319. '</div>',
  320. '</form>'
  321. ].join('\n')).appendTo('body');
  322. $('#eventForm').bootstrapValidator();
  323. this.bv = $('#eventForm').data('bootstrapValidator');
  324. this.$email = this.bv.getFieldElements('email');
  325. });
  326. afterEach(function() {
  327. $('#eventForm').bootstrapValidator('destroy').remove();
  328. });
  329. it('trigger data-bv-emailaddress-onsuccess', function() {
  330. this.$email.val('email@domain.com');
  331. this.bv.validate();
  332. expect($('#msg').html()).toEqual('emailAddress validator passed');
  333. });
  334. it('trigger data-bv-emailaddress-onerror', function() {
  335. this.$email.val('email@domain');
  336. this.bv.validate();
  337. expect($('#msg').html()).toEqual('emailAddress validator did not pass');
  338. });
  339. });
  340. describe('event validator programmatically', function() {
  341. beforeEach(function() {
  342. $([
  343. '<form class="form-horizontal" id="eventForm">',
  344. '<div id="msg"></div>',
  345. '<div class="form-group">',
  346. '<input type="text" name="email" data-bv-emailaddress />',
  347. '</div>',
  348. '</form>'
  349. ].join('\n')).appendTo('body');
  350. $('#eventForm').bootstrapValidator({
  351. fields: {
  352. email: {
  353. validators: {
  354. emailAddress: {
  355. onSuccess: function(e, data) {
  356. $('#msg').html('emailAddress validator: onSuccess() called');
  357. },
  358. onError: function(e, data) {
  359. $('#msg').html('emailAddress validator: onError() called');
  360. },
  361. message: 'The email address is not valid'
  362. }
  363. }
  364. }
  365. }
  366. });
  367. this.bv = $('#eventForm').data('bootstrapValidator');
  368. this.$email = this.bv.getFieldElements('email');
  369. });
  370. afterEach(function() {
  371. $('#eventForm').bootstrapValidator('destroy').remove();
  372. });
  373. it('call onSuccess()', function() {
  374. this.$email.val('email@domain.com');
  375. this.bv.validate();
  376. expect($('#msg').html()).toEqual('emailAddress validator: onSuccess() called');
  377. });
  378. it('call onError()', function() {
  379. this.$email.val('email@domain');
  380. this.bv.validate();
  381. expect($('#msg').html()).toEqual('emailAddress validator: onError() called');
  382. });
  383. });