ソースを参照

#725: Fix the issue when adding field which does not exist but is already set in "fields" option

Phuoc Nguyen 11 年 前
コミット
f2a78e87c1

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@ __Improvements__
 
 __Bug Fixes__
 * [#687](https://github.com/nghuuphuoc/bootstrapvalidator/issues/687), [#711](https://github.com/nghuuphuoc/bootstrapvalidator/pull/711): Keep disabled validators VALID, thanks to [@talberti](https://github.com/talberti)
+* [#725](https://github.com/nghuuphuoc/bootstrapvalidator/pull/725): Fix the issue when adding field which does not exist but is already set in "fields" option
 * [#732](https://github.com/nghuuphuoc/bootstrapvalidator/issues/732): Fix the issue when removing the radio or checkbox field
 
 __Document__

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.2-dev, built on 2014-08-28 10:03:57 AM
+ * @version     v0.5.2-dev, built on 2014-08-28 10:26:11 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 2 - 3
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.2-dev, built on 2014-08-28 10:03:57 AM
+ * @version     v0.5.2-dev, built on 2014-08-28 10:26:11 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -245,7 +245,6 @@
 
             // We don't need to validate non-existing fields
             if (fields.length === 0) {
-                delete this.options.fields[field];
                 return;
             }
 
@@ -756,7 +755,7 @@
                     break;
             }
 
-            if (this.options.fields[field] && this.options.fields[field].enabled === false) {
+            if (fields.length === 0 || (this.options.fields[field] && this.options.fields[field].enabled === false)) {
                 return this;
             }
 

ファイルの差分が大きいため隠しています
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 1 - 2
src/js/bootstrapValidator.js

@@ -244,7 +244,6 @@
 
             // We don't need to validate non-existing fields
             if (fields.length === 0) {
-                delete this.options.fields[field];
                 return;
             }
 
@@ -755,7 +754,7 @@
                     break;
             }
 
-            if (this.options.fields[field] && this.options.fields[field].enabled === false) {
+            if (fields.length === 0 || (this.options.fields[field] && this.options.fields[field].enabled === false)) {
                 return this;
             }
 

+ 74 - 0
test/spec.js

@@ -268,6 +268,80 @@ describe('container tooltip/popover', function() {
     });
 });
 
+describe('dynamic fields', function() {
+    beforeEach(function() {
+        $([
+            '<form class="form-horizontal" id="dynamicForm">',
+                '<div class="form-group">',
+                    '<input type="text" name="fullName" class="form-control" />',
+                '</div>',
+            '</form>'
+        ].join('\n')).appendTo('body');
+
+        $('#dynamicForm').bootstrapValidator({
+            fields: {
+                fullName: {
+                    validators: {
+                        notEmpty: {
+                            message: 'The full name is required and cannot be empty'
+                        },
+                        stringLength: {
+                            min: 8,
+                            max: 40,
+                            message: 'The full name must be more than %s and less than %s characters long'
+                        },
+                        regexp: {
+                            enabled: false,
+                            regexp: /^[a-zA-Z\s]+$/,
+                            message: 'The full name can only consist of alphabetical, number, and space'
+                        }
+                    }
+                },
+                // #725: Note that the email field isn't available in the form yet
+                email: {
+                    validators: {
+                        emailAddress: {
+                            message: 'The email address is not valid'
+                        }
+                    }
+                }
+            }
+        });
+
+        this.bv        = $('#dynamicForm').data('bootstrapValidator');
+        this.$fullName = this.bv.getFieldElements('fullName');
+    });
+
+    afterEach(function() {
+        $('#dynamicForm').bootstrapValidator('destroy').remove();
+    });
+
+    // https://github.com/nghuuphuoc/bootstrapvalidator/pull/725
+    it('adding field [does not exist but is already set in "fields" option]', function() {
+        var $div   = $('<div/>').addClass('form-group').appendTo($('#dynamicForm'));
+            $email = $('<input/>')
+                        .attr('type', 'text')
+                        .addClass('form-control')
+                        .attr('name', 'email')
+                        .appendTo($div);
+
+        this.bv.addField('email');
+
+        this.$fullName.val('Phuoc Nguyen');
+
+        $email.val('not valid@email');
+        this.bv.validate();
+        expect(this.bv.isValidField('email')).toBeFalsy();
+        expect(this.bv.isValid()).toBeFalsy();
+
+        this.bv.resetForm();
+        $email.val('valid@email.com');
+        this.bv.validate();
+        expect(this.bv.isValidField('email')).toBeTruthy();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+});
+
 describe('enable validators', function() {
     beforeEach(function() {
         $([

+ 73 - 0
test/spec/dynamic.js

@@ -0,0 +1,73 @@
+describe('dynamic fields', function() {
+    beforeEach(function() {
+        $([
+            '<form class="form-horizontal" id="dynamicForm">',
+                '<div class="form-group">',
+                    '<input type="text" name="fullName" class="form-control" />',
+                '</div>',
+            '</form>'
+        ].join('\n')).appendTo('body');
+
+        $('#dynamicForm').bootstrapValidator({
+            fields: {
+                fullName: {
+                    validators: {
+                        notEmpty: {
+                            message: 'The full name is required and cannot be empty'
+                        },
+                        stringLength: {
+                            min: 8,
+                            max: 40,
+                            message: 'The full name must be more than %s and less than %s characters long'
+                        },
+                        regexp: {
+                            enabled: false,
+                            regexp: /^[a-zA-Z\s]+$/,
+                            message: 'The full name can only consist of alphabetical, number, and space'
+                        }
+                    }
+                },
+                // #725: Note that the email field isn't available in the form yet
+                email: {
+                    validators: {
+                        emailAddress: {
+                            message: 'The email address is not valid'
+                        }
+                    }
+                }
+            }
+        });
+
+        this.bv        = $('#dynamicForm').data('bootstrapValidator');
+        this.$fullName = this.bv.getFieldElements('fullName');
+    });
+
+    afterEach(function() {
+        $('#dynamicForm').bootstrapValidator('destroy').remove();
+    });
+
+    // https://github.com/nghuuphuoc/bootstrapvalidator/pull/725
+    it('adding field [does not exist but is already set in "fields" option]', function() {
+        var $div   = $('<div/>').addClass('form-group').appendTo($('#dynamicForm'));
+            $email = $('<input/>')
+                        .attr('type', 'text')
+                        .addClass('form-control')
+                        .attr('name', 'email')
+                        .appendTo($div);
+
+        this.bv.addField('email');
+
+        this.$fullName.val('Phuoc Nguyen');
+
+        $email.val('not valid@email');
+        this.bv.validate();
+        expect(this.bv.isValidField('email')).toBeFalsy();
+        expect(this.bv.isValid()).toBeFalsy();
+
+        this.bv.resetForm();
+        $email.val('valid@email.com');
+        this.bv.validate();
+        expect(this.bv.isValidField('email')).toBeTruthy();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+});