excluded.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. describe('excluded', function() {
  2. beforeEach(function() {
  3. $([
  4. '<div class="container">',
  5. '<form class="form-horizontal" id="excludedForm" data-bv-excluded="[name=\'email\']">',
  6. '<div class="form-group">',
  7. '<input type="text" name="username" required />',
  8. '</div>',
  9. '<div class="form-group">',
  10. '<input type="text" name="email" required data-bv-emailaddress />',
  11. '</div>',
  12. '</form>',
  13. '</div>'
  14. ].join('')).appendTo('body');
  15. $('#excludedForm').bootstrapValidator();
  16. this.bv = $('#excludedForm').data('bootstrapValidator');
  17. this.$username = this.bv.getFieldElements('username');
  18. this.$email = this.bv.getFieldElements('email');
  19. });
  20. afterEach(function() {
  21. $('#excludedForm').bootstrapValidator('destroy').parent().remove();
  22. });
  23. it('excluded form declarative', function() {
  24. this.bv.validate();
  25. expect(this.bv.isValid()).toEqual(false);
  26. this.bv.resetForm();
  27. this.$username.val('your_user_name');
  28. this.$email.val('');
  29. this.bv.validate();
  30. expect(this.bv.isValid()).toBeTruthy();
  31. });
  32. it('excluded form programmatically', function() {
  33. this.bv.destroy();
  34. $('#excludedForm').removeAttr('data-bv-excluded');
  35. $('#excludedForm').bootstrapValidator({
  36. excluded: '[name="username"]'
  37. });
  38. this.bv = $('#excludedForm').data('bootstrapValidator');
  39. this.$username = this.bv.getFieldElements('username');
  40. this.$email = this.bv.getFieldElements('email');
  41. this.$username.val('');
  42. this.$email.val('invalid#email.com');
  43. this.bv.validate();
  44. expect(this.bv.isValid()).toEqual(false);
  45. this.bv.resetForm();
  46. this.$email.val('valid@email.com');
  47. this.bv.validate();
  48. expect(this.bv.isValid()).toBeTruthy();
  49. });
  50. it('excluded field declarative', function() {
  51. this.bv.destroy();
  52. $('#excludedForm').removeAttr('data-bv-excluded');
  53. $('#excludedForm').find('[name="username"]').attr('data-bv-excluded', 'true');
  54. $('#excludedForm').find('[name="email"]').attr('data-bv-excluded', 'false');
  55. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  56. this.$username = this.bv.getFieldElements('username');
  57. this.$email = this.bv.getFieldElements('email');
  58. this.$username.val('');
  59. this.$email.val('');
  60. this.bv.validate();
  61. expect(this.bv.isValid()).toEqual(false);
  62. this.bv.resetForm();
  63. this.$email.val('invalid#email.com');
  64. this.bv.validate();
  65. expect(this.bv.isValid()).toEqual(false);
  66. this.bv.resetForm();
  67. this.$email.val('valid@email.com');
  68. this.bv.validate();
  69. expect(this.bv.isValid()).toBeTruthy();
  70. });
  71. it('excluded field programmatically true/false', function() {
  72. this.bv.destroy();
  73. $('#excludedForm').removeAttr('data-bv-excluded');
  74. $('#excludedForm').bootstrapValidator({
  75. fields: {
  76. username: {
  77. excluded: true
  78. },
  79. email: {
  80. excluded: false
  81. }
  82. }
  83. });
  84. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  85. this.$username = this.bv.getFieldElements('username');
  86. this.$email = this.bv.getFieldElements('email');
  87. this.$username.val('');
  88. this.$email.val('');
  89. this.bv.validate();
  90. expect(this.bv.isValid()).toEqual(false);
  91. this.bv.resetForm();
  92. this.$email.val('invalid#email.com');
  93. this.bv.validate();
  94. expect(this.bv.isValid()).toEqual(false);
  95. this.bv.resetForm();
  96. this.$email.val('valid@email.com');
  97. this.bv.validate();
  98. expect(this.bv.isValid()).toBeTruthy();
  99. });
  100. it('excluded field programmatically "true"/"false"', function() {
  101. this.bv.destroy();
  102. $('#excludedForm').removeAttr('data-bv-excluded');
  103. $('#excludedForm').bootstrapValidator({
  104. fields: {
  105. username: {
  106. excluded: 'false'
  107. },
  108. email: {
  109. excluded: 'true'
  110. }
  111. }
  112. });
  113. this.bv = $('#excludedForm').bootstrapValidator().data('bootstrapValidator');
  114. this.$username = this.bv.getFieldElements('username');
  115. this.$email = this.bv.getFieldElements('email');
  116. this.$username.val('');
  117. this.$email.val('valid@email.com');
  118. this.bv.validate();
  119. expect(this.bv.isValid()).toEqual(false);
  120. this.bv.resetForm();
  121. this.$username.val('your_user_name');
  122. this.$email.val('invalid#email.com');
  123. this.bv.validate();
  124. expect(this.bv.isValid()).toBeTruthy();
  125. });
  126. });