Browse Source

#80: Add enabled option and enableFieldValidators() method to enabled/disable field validators

nghuuphuoc 11 years ago
parent
commit
8f7b6f2389
5 changed files with 312 additions and 6 deletions
  1. 1 1
      README.md
  2. 208 0
      demo/enable.html
  3. 51 2
      dist/js/bootstrapValidator.js
  4. 1 1
      dist/js/bootstrapValidator.min.js
  5. 51 2
      src/js/bootstrapValidator.js

+ 1 - 1
README.md

@@ -190,7 +190,7 @@ uri                                     | Validate an URL address
 
 The validator options are described in the following section:
 
-(**The options masked with * are required**)
+(**The options marked with * are required**)
 
 ### between validator
 

+ 208 - 0
demo/enable.html

@@ -0,0 +1,208 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>BootstrapValidator demo</title>
+
+    <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
+    <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
+
+    <script type="text/javascript" src="../vendor/jquery/jquery-1.10.2.min.js"></script>
+    <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
+    <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
+</head>
+<body>
+    <div class="container">
+        <div class="row">
+            <!-- form: -->
+            <section>
+                <div class="col-lg-8 col-lg-offset-2">
+                    <div class="page-header">
+                        <h2>Checkout</h2>
+                    </div>
+
+                    <form id="checkoutForm" method="post" class="form-horizontal" action="target.php">
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">Shipping address</label>
+                            <div class="col-lg-5">
+                                <div class="radio">
+                                    <label>
+                                        <input type="radio" name="shipping_to" value="current" checked /> Same as my account address
+                                    </label>
+                                </div>
+                                <div class="radio">
+                                    <label>
+                                        <input type="radio" name="shipping_to" value="new" /> New one as following
+                                    </label>
+                                </div>
+                            </div>
+                        </div>
+
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">Country</label>
+                            <div class="col-lg-5">
+                                <select name="country" class="form-control">
+                                    <option value="">Select a country</option>
+                                    <option value="AU">Australia</option>
+                                    <option value="CA">Canada</option>
+                                    <option value="FR">France</option>
+                                    <option value="DE">Germany</option>
+                                    <option value="IT">Italy</option>
+                                    <option value="JP">Japan</option>
+                                    <option value="GB">United Kingdom</option>
+                                    <option value="US">United States</option>
+                                    <option value="VN">Viet Nam</option>
+                                </select>
+                            </div>
+                        </div>
+
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">Street</label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" name="street" />
+                            </div>
+                        </div>
+
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">City</label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" name="city" />
+                            </div>
+                        </div>
+
+                        <div class="form-group">
+                            <label class="col-lg-3 control-label">State</label>
+                            <div class="col-lg-5">
+                                <select name="state" class="form-control">
+                                    <option value="">Select a state</option>
+                                    <option value="AL">Alabama</option>
+                                    <option value="AK">Alaska</option>
+                                    <option value="AZ">Arizona</option>
+                                    <option value="AR">Arkansas</option>
+                                    <option value="CA">California</option>
+                                    <option value="CO">Colorado</option>
+                                    <option value="CT">Connecticut</option>
+                                    <option value="DE">Delaware</option>
+                                    <option value="DC">District of Columbia</option>
+                                    <option value="FL">Florida</option>
+                                    <option value="GA">Georgia</option>
+                                    <option value="HI">Hawaii</option>
+                                    <option value="ID">Idaho</option>
+                                    <option value="IL">Illinois</option>
+                                    <option value="IN">Indiana</option>
+                                    <option value="IA">Iowa</option>
+                                    <option value="KS">Kansas</option>
+                                    <option value="KY">Kentucky</option>
+                                    <option value="LA">Louisiana</option>
+                                    <option value="ME">Maine</option>
+                                    <option value="MD">Maryland</option>
+                                    <option value="MA">Massachusetts</option>
+                                    <option value="MI">Michigan</option>
+                                    <option value="MN">Minnesota</option>
+                                    <option value="MS">Mississippi</option>
+                                    <option value="MO">Missouri</option>
+                                    <option value="MT">Montana</option>
+                                    <option value="NE">Nebraska</option>
+                                    <option value="NV">Nevada</option>
+                                    <option value="NH">New Hampshire</option>
+                                    <option value="NJ">New Jersey</option>
+                                    <option value="NM">New Mexico</option>
+                                    <option value="NY">New York</option>
+                                    <option value="NC">North Carolina</option>
+                                    <option value="ND">North Dakota</option>
+                                    <option value="OH">Ohio</option>
+                                    <option value="OK">Oklahoma</option>
+                                    <option value="OR">Oregon</option>
+                                    <option value="PA">Pennsylvania</option>
+                                    <option value="RI">Rhode Island</option>
+                                    <option value="SC">South Carolina</option>
+                                    <option value="SD">South Dakota</option>
+                                    <option value="TN">Tennessee</option>
+                                    <option value="TX">Texas</option>
+                                    <option value="UT">Utah</option>
+                                    <option value="VT">Vermont</option>
+                                    <option value="VA">Virginia</option>
+                                    <option value="WA">Washington</option>
+                                    <option value="WV">West Virginia</option>
+                                    <option value="WI">Wisconsin</option>
+                                    <option value="WY">Wyoming</option>
+                                </select>
+                                <span class="help-block"><small>Required if you choose United States country</small></span>
+                            </div>
+                        </div>
+
+                        <div class="form-group">
+                            <div class="col-lg-9 col-lg-offset-3">
+                                <button type="submit" class="btn btn-primary">Checkout</button>
+                            </div>
+                        </div>
+                    </form>
+                </div>
+            </section>
+            <!-- :form -->
+        </div>
+    </div>
+
+<script type="text/javascript">
+$(document).ready(function() {
+    $('#checkoutForm').bootstrapValidator({
+        message: 'This value is not valid',
+        feedbackIcons: {
+            valid: 'glyphicon glyphicon-ok',
+            invalid: 'glyphicon glyphicon-remove',
+            validating: 'glyphicon glyphicon-refresh'
+        },
+        fields: {
+            street: {
+                enabled: false,
+                validators: {
+                    notEmpty: {
+                        message: 'The street is required and cannot be empty'
+                    }
+                }
+            },
+            city: {
+                enabled: false,
+                validators: {
+                    notEmpty: {
+                        message: 'The city is required and cannot be empty'
+                    }
+                }
+            },
+            country: {
+                enabled: false,
+                validators: {
+                    notEmpty: {
+                        message: 'The country is required and cannot be empty'
+                    }
+                }
+            },
+            state: {
+                enabled: false,
+                validators: {
+                    notEmpty: {
+                        message: 'The state is required and cannot be empty'
+                    }
+                }
+            }
+        }
+    });
+
+    // Enable street/city/country validators if user want to ship to other address
+    $('input[name="shipping_to"]').on('change', function() {
+        var bootstrapValidator = $('#checkoutForm').data('bootstrapValidator'),
+            shipNewAddress     = ($(this).val() == 'new');
+
+        bootstrapValidator.enableFieldValidators('street', shipNewAddress)
+                          .enableFieldValidators('city', shipNewAddress)
+                          .enableFieldValidators('country', shipNewAddress);
+    });
+
+    // Require state if user choose US country
+    $('select[name="country"]').on('change', function() {
+        var bootstrapValidator = $('#checkoutForm').data('bootstrapValidator');
+        bootstrapValidator.enableFieldValidators('state', $(this).val() == 'US');
+    });
+});
+</script>
+</body>
+</html>

+ 51 - 2
dist/js/bootstrapValidator.js

@@ -196,7 +196,11 @@
                 $('<i/>').css('display', 'none').addClass('form-control-feedback').insertAfter($(fields[fields.length - 1]));
             }
 
-            // Whenever the user change the field value, make it as not validated yet
+            if (this.options.fields[field]['enabled'] == null) {
+                this.options.fields[field]['enabled'] = true;
+            }
+
+            // Whenever the user change the field value, mark it as not validated yet
             var that  = this,
                 type  = fields.attr('type'),
                 event = ('radio' == type || 'checkbox' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup';
@@ -286,6 +290,8 @@
 
         /**
          * Validate the form
+         *
+         * @return {BootstrapValidator}
          */
         validate: function() {
             if (!this.options.fields) {
@@ -307,6 +313,10 @@
          * @param {String} field The field name
          */
         validateField: function(field) {
+            if (!this.options.fields[field]['enabled']) {
+                return;
+            }
+
             var that       = this,
                 $field     = $(this.getFieldElements(field)[0]),
                 validators = this.options.fields[field].validators,
@@ -351,6 +361,10 @@
         isValid: function() {
             var field, validatorName;
             for (field in this.results) {
+                if (!this.options.fields[field]['enabled']) {
+                    continue;
+                }
+
                 for (validatorName in this.results[field]) {
                     if (this.results[field][validatorName] == this.STATUS_NOT_VALIDATED || this.results[field][validatorName] == this.STATUS_VALIDATING) {
                         return false;
@@ -373,6 +387,7 @@
          * @param {String} validatorName
          * @param {String} status The status
          * Can be STATUS_VALIDATING, STATUS_INVALID, STATUS_VALID
+         * @return {BootstrapValidator}
          */
         updateStatus: function($field, validatorName, status) {
             var that      = this,
@@ -437,6 +452,8 @@
                 default:
                     break;
             }
+
+            return this;
         },
 
         // Useful APIs which aren't used internally
@@ -445,6 +462,7 @@
          * Reset the form
          *
          * @param {Boolean} resetFormData Reset current form data
+         * @return {BootstrapValidator}
          */
         resetForm: function(resetFormData) {
             for (var field in this.options.fields) {
@@ -469,7 +487,7 @@
             // Enable submit buttons
             this._disableSubmitButtons(false);
 
-            // Hide all feeback icons
+            // Hide all feedback icons
             if (this.options.feedbackIcons) {
                 this.$form.find('.form-control-feedback').removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
             }
@@ -477,6 +495,37 @@
             if (resetFormData) {
                 this.$form.get(0).reset();
             }
+
+            return this;
+        },
+
+        /**
+         * Enable/Disable all validators to given field
+         *
+         * @param {String} field The field name
+         * @param {Boolean} enabled Enable/Disable field validators
+         * @return {BootstrapValidator}
+         */
+        enableFieldValidators: function(field, enabled) {
+            this.options.fields[field]['enabled'] = enabled;
+            if (!enabled) {
+                // this.results[field] = {};
+                for (var v in this.options.fields[field].validators) {
+                    this.results[field][v] = this.STATUS_NOT_VALIDATED;
+                }
+
+                var $field  = this.getFieldElements(field),
+                    $parent = $field.parents('.form-group');
+
+                $parent.removeClass('has-success has-error').find('.help-block[data-bs-validator]').hide();
+                if (this.options.feedbackIcons) {
+                    $parent.find('.form-control-feedback').removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid).hide();
+                }
+
+                this._disableSubmitButtons(false);
+            }
+
+            return this;
         }
     };
 

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


+ 51 - 2
src/js/bootstrapValidator.js

@@ -195,7 +195,11 @@
                 $('<i/>').css('display', 'none').addClass('form-control-feedback').insertAfter($(fields[fields.length - 1]));
             }
 
-            // Whenever the user change the field value, make it as not validated yet
+            if (this.options.fields[field]['enabled'] == null) {
+                this.options.fields[field]['enabled'] = true;
+            }
+
+            // Whenever the user change the field value, mark it as not validated yet
             var that  = this,
                 type  = fields.attr('type'),
                 event = ('radio' == type || 'checkbox' == type || 'SELECT' == fields[0].tagName) ? 'change' : 'keyup';
@@ -285,6 +289,8 @@
 
         /**
          * Validate the form
+         *
+         * @return {BootstrapValidator}
          */
         validate: function() {
             if (!this.options.fields) {
@@ -306,6 +312,10 @@
          * @param {String} field The field name
          */
         validateField: function(field) {
+            if (!this.options.fields[field]['enabled']) {
+                return;
+            }
+
             var that       = this,
                 $field     = $(this.getFieldElements(field)[0]),
                 validators = this.options.fields[field].validators,
@@ -350,6 +360,10 @@
         isValid: function() {
             var field, validatorName;
             for (field in this.results) {
+                if (!this.options.fields[field]['enabled']) {
+                    continue;
+                }
+
                 for (validatorName in this.results[field]) {
                     if (this.results[field][validatorName] == this.STATUS_NOT_VALIDATED || this.results[field][validatorName] == this.STATUS_VALIDATING) {
                         return false;
@@ -372,6 +386,7 @@
          * @param {String} validatorName
          * @param {String} status The status
          * Can be STATUS_VALIDATING, STATUS_INVALID, STATUS_VALID
+         * @return {BootstrapValidator}
          */
         updateStatus: function($field, validatorName, status) {
             var that      = this,
@@ -436,6 +451,8 @@
                 default:
                     break;
             }
+
+            return this;
         },
 
         // Useful APIs which aren't used internally
@@ -444,6 +461,7 @@
          * Reset the form
          *
          * @param {Boolean} resetFormData Reset current form data
+         * @return {BootstrapValidator}
          */
         resetForm: function(resetFormData) {
             for (var field in this.options.fields) {
@@ -468,7 +486,7 @@
             // Enable submit buttons
             this._disableSubmitButtons(false);
 
-            // Hide all feeback icons
+            // Hide all feedback icons
             if (this.options.feedbackIcons) {
                 this.$form.find('.form-control-feedback').removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).hide();
             }
@@ -476,6 +494,37 @@
             if (resetFormData) {
                 this.$form.get(0).reset();
             }
+
+            return this;
+        },
+
+        /**
+         * Enable/Disable all validators to given field
+         *
+         * @param {String} field The field name
+         * @param {Boolean} enabled Enable/Disable field validators
+         * @return {BootstrapValidator}
+         */
+        enableFieldValidators: function(field, enabled) {
+            this.options.fields[field]['enabled'] = enabled;
+            if (!enabled) {
+                // this.results[field] = {};
+                for (var v in this.options.fields[field].validators) {
+                    this.results[field][v] = this.STATUS_NOT_VALIDATED;
+                }
+
+                var $field  = this.getFieldElements(field),
+                    $parent = $field.parents('.form-group');
+
+                $parent.removeClass('has-success has-error').find('.help-block[data-bs-validator]').hide();
+                if (this.options.feedbackIcons) {
+                    $parent.find('.form-control-feedback').removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).removeClass(this.options.feedbackIcons.valid).hide();
+                }
+
+                this._disableSubmitButtons(false);
+            }
+
+            return this;
         }
     };