dynamic.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. describe('dynamic fields', function() {
  2. beforeEach(function() {
  3. $([
  4. '<form class="form-horizontal" id="dynamicForm">',
  5. '<div class="form-group">',
  6. '<input type="text" name="fullName" class="form-control" />',
  7. '</div>',
  8. '</form>'
  9. ].join('\n')).appendTo('body');
  10. $('#dynamicForm').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. // #725: Note that the email field isn't available in the form yet
  30. email: {
  31. validators: {
  32. emailAddress: {
  33. message: 'The email address is not valid'
  34. }
  35. }
  36. }
  37. }
  38. });
  39. this.bv = $('#dynamicForm').data('bootstrapValidator');
  40. this.$fullName = this.bv.getFieldElements('fullName');
  41. });
  42. afterEach(function() {
  43. $('#dynamicForm').bootstrapValidator('destroy').remove();
  44. });
  45. // https://github.com/nghuuphuoc/bootstrapvalidator/pull/725
  46. it('adding field [does not exist but is already set in "fields" option]', function() {
  47. var $div = $('<div/>').addClass('form-group').appendTo($('#dynamicForm'));
  48. $email = $('<input/>')
  49. .attr('type', 'text')
  50. .addClass('form-control')
  51. .attr('name', 'email')
  52. .appendTo($div);
  53. this.bv.addField('email');
  54. this.$fullName.val('Phuoc Nguyen');
  55. $email.val('not valid@email');
  56. this.bv.validate();
  57. expect(this.bv.isValidField('email')).toBeFalsy();
  58. expect(this.bv.isValid()).toBeFalsy();
  59. this.bv.resetForm();
  60. $email.val('valid@email.com');
  61. this.bv.validate();
  62. expect(this.bv.isValidField('email')).toBeTruthy();
  63. expect(this.bv.isValid()).toBeTruthy();
  64. });
  65. });