浏览代码

API: Added getOptions: function(field, validator, option) method

phuoc 11 年之前
父节点
当前提交
ee31976fac
共有 6 个文件被更改,包括 114 次插入32 次删除
  1. 1 1
      dist/css/bootstrapValidator.min.css
  2. 31 1
      dist/js/bootstrapValidator.js
  3. 4 4
      dist/js/bootstrapValidator.min.js
  4. 30 0
      src/js/bootstrapValidator.js
  5. 24 13
      test/spec.js
  6. 24 13
      test/spec/api.js

+ 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.0-dev, built on 2014-07-08 4:05:10 PM
+ * @version     v0.5.0-dev, built on 2014-07-09 9:40:22 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 31 - 1
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.0-dev, built on 2014-07-08 4:05:10 PM
+ * @version     v0.5.0-dev, built on 2014-07-09 9:40:22 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -1158,6 +1158,36 @@
         },
 
         /**
+         * Get the field options
+         *
+         * @param {String|jQuery} [field] The field name or field element. If it is not set, the method returns the form options
+         * @param {String} [validator] The name of validator. It null, the method returns form options
+         * @param {String} [option] The option name
+         * @return {String|Object}
+         */
+        getOptions: function(field, validator, option) {
+            if (!field) {
+                return this.options;
+            }
+            if ('object' === typeof field) {
+                field = field.attr('data-bv-field');
+            }
+            if (!this.options.fields[field]) {
+                return null;
+            }
+
+            var options = this.options.fields[field];
+            if (!validator) {
+                return options;
+            }
+            if (!options.validators || !options.validators[validator]) {
+                return null;
+            }
+
+            return option ? options.validators[validator][option] : options.validators[validator];
+        },
+
+        /**
          * Update the option of a specific validator
          *
          * @param {String|jQuery} field The field name or field element

文件差异内容过多而无法显示
+ 4 - 4
dist/js/bootstrapValidator.min.js


+ 30 - 0
src/js/bootstrapValidator.js

@@ -1157,6 +1157,36 @@
         },
 
         /**
+         * Get the field options
+         *
+         * @param {String|jQuery} [field] The field name or field element. If it is not set, the method returns the form options
+         * @param {String} [validator] The name of validator. It null, the method returns form options
+         * @param {String} [option] The option name
+         * @return {String|Object}
+         */
+        getOptions: function(field, validator, option) {
+            if (!field) {
+                return this.options;
+            }
+            if ('object' === typeof field) {
+                field = field.attr('data-bv-field');
+            }
+            if (!this.options.fields[field]) {
+                return null;
+            }
+
+            var options = this.options.fields[field];
+            if (!validator) {
+                return options;
+            }
+            if (!options.validators || !options.validators[validator]) {
+                return null;
+            }
+
+            return option ? options.validators[validator][option] : options.validators[validator];
+        },
+
+        /**
          * Update the option of a specific validator
          *
          * @param {String|jQuery} field The field name or field element

+ 24 - 13
test/spec.js

@@ -10,16 +10,14 @@ describe('api', function() {
 
     beforeEach(function() {
         $([
-            '<div class="container">',
-                '<form class="form-horizontal" id="apiForm">',
-                    '<div class="form-group">',
-                        '<input type="text" name="username" data-bv-notempty />',
-                    '</div>',
-                    '<div class="form-group">',
-                        '<input type="text" name="email" data-bv-emailaddress />',
-                    '</div>',
-                '</form>',
-            '</div>'
+            '<form class="form-horizontal" id="apiForm">',
+                '<div class="form-group">',
+                    '<input type="text" name="username" data-bv-notempty data-bv-stringlength data-bv-stringlength-min="8" />',
+                '</div>',
+                '<div class="form-group">',
+                    '<input type="text" name="email" data-bv-notempty data-bv-emailaddress />',
+                '</div>',
+            '</form>'
         ].join('\n')).appendTo('body');
 
         $('#apiForm').bootstrapValidator();
@@ -29,10 +27,10 @@ describe('api', function() {
     });
 
     afterEach(function() {
-        $('#apiForm').bootstrapValidator('destroy').parent().remove();
+        $('#apiForm').bootstrapValidator('destroy').remove();
     });
 
-    it('call revalidateField()', function() {
+    it('revalidateField()', function() {
         this.$email.val('email@domain.com');
         this.bv.validate();
         expect(this.bv.isValidField('email')).toBeTruthy();
@@ -42,7 +40,7 @@ describe('api', function() {
         expect(this.bv.isValidField(this.$email)).toEqual(false);
     });
 
-    it('call destroy()', function() {
+    it('destroy()', function() {
         this.bv.destroy();
         expect($('#apiForm').data('bootstrapValidator')).toBeUndefined();
         expect($('#apiForm').find('i[data-bv-icon-for]').length).toEqual(0);
@@ -52,6 +50,19 @@ describe('api', function() {
         expect($('#apiForm').find('.has-error').length).toEqual(0);
         expect($('#apiForm').find('[data-bv-field]').length).toEqual(0);
     });
+
+    it('getOptions()', function() {
+        // Form options
+        expect(this.bv.getOptions().feedbackIcons.valid).toEqual('glyphicon glyphicon-ok');
+
+        // Field options
+        expect(this.bv.getOptions('username', 'stringlength')).toBeNull();
+        expect(this.bv.getOptions('username', 'stringlength', 'min')).toBeNull();
+
+        expect(this.bv.getOptions('username', 'stringLength')).toBeDefined();
+        expect(this.bv.getOptions('username', 'stringLength', 'min')).toEqual('8');
+        expect(this.bv.getOptions('username', 'stringlength', 'max')).toBeNull();
+    });
 });
 
 describe('container form option', function() {

+ 24 - 13
test/spec/api.js

@@ -10,16 +10,14 @@ describe('api', function() {
 
     beforeEach(function() {
         $([
-            '<div class="container">',
-                '<form class="form-horizontal" id="apiForm">',
-                    '<div class="form-group">',
-                        '<input type="text" name="username" data-bv-notempty />',
-                    '</div>',
-                    '<div class="form-group">',
-                        '<input type="text" name="email" data-bv-emailaddress />',
-                    '</div>',
-                '</form>',
-            '</div>'
+            '<form class="form-horizontal" id="apiForm">',
+                '<div class="form-group">',
+                    '<input type="text" name="username" data-bv-notempty data-bv-stringlength data-bv-stringlength-min="8" />',
+                '</div>',
+                '<div class="form-group">',
+                    '<input type="text" name="email" data-bv-notempty data-bv-emailaddress />',
+                '</div>',
+            '</form>'
         ].join('\n')).appendTo('body');
 
         $('#apiForm').bootstrapValidator();
@@ -29,10 +27,10 @@ describe('api', function() {
     });
 
     afterEach(function() {
-        $('#apiForm').bootstrapValidator('destroy').parent().remove();
+        $('#apiForm').bootstrapValidator('destroy').remove();
     });
 
-    it('call revalidateField()', function() {
+    it('revalidateField()', function() {
         this.$email.val('email@domain.com');
         this.bv.validate();
         expect(this.bv.isValidField('email')).toBeTruthy();
@@ -42,7 +40,7 @@ describe('api', function() {
         expect(this.bv.isValidField(this.$email)).toEqual(false);
     });
 
-    it('call destroy()', function() {
+    it('destroy()', function() {
         this.bv.destroy();
         expect($('#apiForm').data('bootstrapValidator')).toBeUndefined();
         expect($('#apiForm').find('i[data-bv-icon-for]').length).toEqual(0);
@@ -52,4 +50,17 @@ describe('api', function() {
         expect($('#apiForm').find('.has-error').length).toEqual(0);
         expect($('#apiForm').find('[data-bv-field]').length).toEqual(0);
     });
+
+    it('getOptions()', function() {
+        // Form options
+        expect(this.bv.getOptions().feedbackIcons.valid).toEqual('glyphicon glyphicon-ok');
+
+        // Field options
+        expect(this.bv.getOptions('username', 'stringlength')).toBeNull();
+        expect(this.bv.getOptions('username', 'stringlength', 'min')).toBeNull();
+
+        expect(this.bv.getOptions('username', 'stringLength')).toBeDefined();
+        expect(this.bv.getOptions('username', 'stringLength', 'min')).toEqual('8');
+        expect(this.bv.getOptions('username', 'stringlength', 'max')).toBeNull();
+    });
 });