Browse Source

#323: Add isValidField(field) method

nghuuphuoc 11 years ago
parent
commit
540984ddb1
4 changed files with 90 additions and 41 deletions
  1. 2 1
      CHANGELOG.md
  2. 43 19
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 43 19
      src/js/bootstrapValidator.js

+ 2 - 1
CHANGELOG.md

@@ -11,7 +11,8 @@
 * [#130](https://github.com/nghuuphuoc/bootstrapvalidator/pull/130): Add ```addField()``` and ```removeField()``` methods for managing dynamic fields, thanks to [@jcnmulio](https://github.com/jcnmulio)
 * [#211](https://github.com/nghuuphuoc/bootstrapvalidator/issues/211), [#235](https://github.com/nghuuphuoc/bootstrapvalidator/issues/235): Add new method ```getInvalidFields()``` that returns all invalid fields
 * [#275](https://github.com/nghuuphuoc/bootstrapvalidator/issues/275): Add ```destroy()``` method
-* [#316](https://github.com/nghuuphuoc/bootstrapvalidator/issues/316): Add ```isValidContainer()``` method
+* [#316](https://github.com/nghuuphuoc/bootstrapvalidator/issues/316): Add ```isValidContainer(container)``` method
+* [#323](https://github.com/nghuuphuoc/bootstrapvalidator/issues/323): Add ```isValidField(field)``` method
 * [#297](https://github.com/nghuuphuoc/bootstrapvalidator/issues/297): Disable feedback icons for particular fields
 * [#244](https://github.com/nghuuphuoc/bootstrapvalidator/pull/244): Only enable the submit buttons if all fields are valid, thanks to [@smeagol74](https://github.com/smeagol74)
 * [#274](https://github.com/nghuuphuoc/bootstrapvalidator/pull/274): Fix feedback icons in ```input-group```, thanks to [@tiagofontella](https://github.com/tiagofontella)

+ 43 - 19
dist/js/bootstrapValidator.js

@@ -883,29 +883,53 @@
          * @returns {Boolean}
          */
         isValid: function() {
-            var fields, field, $field,
-                type, status, validatorName,
-                n, i;
-            for (field in this.options.fields) {
-                if (this.options.fields[field] == null || this.options.fields[field]['enabled'] == false) {
-                    continue;
+            for (var field in this.options.fields) {
+                if (!this.isValidField(field)) {
+                    return false;
                 }
+            }
 
-                fields = this.getFieldElements(field);
-                type   = fields.attr('type');
-                n      = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
+            return true;
+        },
 
-                for (i = 0; i < n; i++) {
-                    $field = $(fields[i]);
-                    if (this._isExcluded($field)) {
-                        continue;
-                    }
+        /**
+         * Check if the field is valid or not
+         *
+         * @param {String|jQuery} field Can be
+         * - the field name
+         * - or an jQuery object representing the field
+         * @returns {Boolean}
+         */
+        isValidField: function(field) {
+            var fields = $([]);
+            switch (typeof field) {
+                case 'object':
+                    fields = field;
+                    field  = field.attr('data-bv-field');
+                    break;
+                case 'string':
+                    fields = this.getFieldElements(field);
+                    break;
+                default:
+                    break;
+            }
+            if (fields.length == 0 || this.options.fields[field] == null || this.options.fields[field]['enabled'] == false) {
+                return true;
+            }
 
-                    for (validatorName in this.options.fields[field].validators) {
-                        status = $field.data('bv.result.' + validatorName);
-                        if (status != this.STATUS_VALID) {
-                            return false;
-                        }
+            var type = fields.attr('type'),
+                n    = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length,
+                $field, validatorName, status;
+            for (var i = 0; i < n; i++) {
+                $field = $(fields[i]);
+                if (this._isExcluded($field)) {
+                    continue;
+                }
+
+                for (validatorName in this.options.fields[field].validators) {
+                    status = $field.data('bv.result.' + validatorName);
+                    if (status != this.STATUS_VALID) {
+                        return false;
                     }
                 }
             }

File diff suppressed because it is too large
+ 2 - 2
dist/js/bootstrapValidator.min.js


+ 43 - 19
src/js/bootstrapValidator.js

@@ -882,29 +882,53 @@
          * @returns {Boolean}
          */
         isValid: function() {
-            var fields, field, $field,
-                type, status, validatorName,
-                n, i;
-            for (field in this.options.fields) {
-                if (this.options.fields[field] == null || this.options.fields[field]['enabled'] == false) {
-                    continue;
+            for (var field in this.options.fields) {
+                if (!this.isValidField(field)) {
+                    return false;
                 }
+            }
 
-                fields = this.getFieldElements(field);
-                type   = fields.attr('type');
-                n      = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length;
+            return true;
+        },
 
-                for (i = 0; i < n; i++) {
-                    $field = $(fields[i]);
-                    if (this._isExcluded($field)) {
-                        continue;
-                    }
+        /**
+         * Check if the field is valid or not
+         *
+         * @param {String|jQuery} field Can be
+         * - the field name
+         * - or an jQuery object representing the field
+         * @returns {Boolean}
+         */
+        isValidField: function(field) {
+            var fields = $([]);
+            switch (typeof field) {
+                case 'object':
+                    fields = field;
+                    field  = field.attr('data-bv-field');
+                    break;
+                case 'string':
+                    fields = this.getFieldElements(field);
+                    break;
+                default:
+                    break;
+            }
+            if (fields.length == 0 || this.options.fields[field] == null || this.options.fields[field]['enabled'] == false) {
+                return true;
+            }
 
-                    for (validatorName in this.options.fields[field].validators) {
-                        status = $field.data('bv.result.' + validatorName);
-                        if (status != this.STATUS_VALID) {
-                            return false;
-                        }
+            var type = fields.attr('type'),
+                n    = (('radio' == type) || ('checkbox' == type)) ? 1 : fields.length,
+                $field, validatorName, status;
+            for (var i = 0; i < n; i++) {
+                $field = $(fields[i]);
+                if (this._isExcluded($field)) {
+                    continue;
+                }
+
+                for (validatorName in this.options.fields[field].validators) {
+                    status = $field.data('bv.result.' + validatorName);
+                    if (status != this.STATUS_VALID) {
+                        return false;
                     }
                 }
             }