enable.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. describe('enable validators', function() {
  2. beforeEach(function() {
  3. $([
  4. '<form class="form-horizontal" id="enableForm">',
  5. '<div class="form-group">',
  6. '<input type="text" name="fullName" class="form-control" />',
  7. '</div>',
  8. '</form>'
  9. ].join('\n')).appendTo('body');
  10. $('#enableForm').bootstrapValidator({
  11. fields: {
  12. fullName: {
  13. validators: {
  14. notEmpty: {
  15. message: 'The full name is required and cannot be empty'
  16. },
  17. stringLength: {
  18. min: 8,
  19. max: 40,
  20. message: 'The full name must be more than %s and less than %s characters long'
  21. },
  22. regexp: {
  23. enabled: false,
  24. regexp: /^[a-zA-Z\s]+$/,
  25. message: 'The full name can only consist of alphabetical, number, and space'
  26. }
  27. }
  28. }
  29. }
  30. });
  31. this.bv = $('#enableForm').data('bootstrapValidator');
  32. this.$fullName = this.bv.getFieldElements('fullName');
  33. });
  34. afterEach(function() {
  35. $('#enableForm').bootstrapValidator('destroy').remove();
  36. });
  37. it('enable all validators', function() {
  38. this.$fullName.val('@ $full N@m3');
  39. this.bv.validate();
  40. expect(this.bv.isValid()).toBeTruthy();
  41. this.bv.resetForm();
  42. this.$fullName.val('Contain#$@');
  43. this.bv.enableFieldValidators('fullName', true);
  44. this.bv.validate();
  45. expect(this.bv.isValidField('fullName')).toEqual(false);
  46. expect(this.bv.isValid()).toEqual(false);
  47. });
  48. it('disable all validators', function() {
  49. this.bv.resetForm();
  50. this.bv.enableFieldValidators('fullName', false);
  51. this.bv.validate();
  52. expect(this.bv.isValid()).toBeTruthy();
  53. });
  54. it('enabled option particular validator', function() {
  55. this.$fullName.val('Contain@#$');
  56. this.bv.validate();
  57. expect(this.bv.isValid()).toBeTruthy();
  58. var messages = this.bv.getMessages('fullName');
  59. expect(messages.length).toEqual(0);
  60. });
  61. it('enable particular validators', function() {
  62. // Enable stringLength validator
  63. this.bv.resetForm();
  64. this.bv.enableFieldValidators('fullName', true, 'stringLength');
  65. this.bv.enableFieldValidators('fullName', true, 'regexp');
  66. this.$fullName.val('Full@');
  67. this.bv.validate();
  68. expect(this.bv.isValid()).toEqual(false);
  69. var messages = this.bv.getMessages('fullName');
  70. expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toBeGreaterThan(-1);
  71. expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toBeGreaterThan(-1);
  72. });
  73. it('disable particular validators', function() {
  74. // Disable stringLength validator
  75. this.bv.enableFieldValidators('fullName', false, 'stringLength');
  76. this.$fullName.val('Full');
  77. this.bv.validate();
  78. expect(this.bv.isValid()).toBeTruthy();
  79. var messages = this.bv.getMessages('fullName');
  80. expect($.inArray('The full name must be more than 8 and less than 40 characters long', messages)).toEqual(-1);
  81. // Disable regexp validator
  82. this.bv.enableFieldValidators('fullName', false, 'regexp');
  83. this.$fullName.val('Special@#$');
  84. this.bv.validate();
  85. expect(this.bv.isValid()).toBeTruthy();
  86. var messages = this.bv.getMessages('fullName');
  87. expect($.inArray('The full name can only consist of alphabetical, number, and space', messages)).toEqual(-1);
  88. });
  89. });