浏览代码

#48: Support custom feedback icons

nghuuphuoc 11 年之前
父节点
当前提交
5a6e5345fe
共有 5 个文件被更改,包括 121 次插入80 次删除
  1. 72 47
      README.md
  2. 8 1
      demo/index.html
  3. 20 25
      dist/js/bootstrapValidator.js
  4. 1 1
      dist/js/bootstrapValidator.min.js
  5. 20 6
      src/js/bootstrapValidator.js

+ 72 - 47
README.md

@@ -65,60 +65,85 @@ Call the plugin to validate the form as following:
 ```javascript
 $(document).ready(function() {
     $(<form Selector>).bootstrapValidator({
-        // The default error message for all fields
-        // You can specify the error message for any fields
         message: 'This value is not valid',
-
-        // The number of grid columns
-        // Change it if you use custom grid with different number of columns
         columns: 12,
-
-        // Shows ok/error/validating icons based on the field validity.
-        // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
-        // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
-        // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
-        feedbackIcons: false,
-
-        // The submit buttons selector
-        // These buttons will be disabled when the form input are invalid
-        submitButtons: ...,
-
-        // Custom submit handler
-        // The handler has three arguments:
-        // - validator is the instance of BootstrapValidator
-        // - form is jQuery object representing the current form
-        // - submitButton is jQuery object representing the submit button which is clicked
-        // By default, submitHandler is null
-        submitHandler: function(validator, form, submitButton) {
-        },
-
-        // Live validating. Can be one of 3 values:
-        // - enabled: The plugin validates fields as soon as they are changed
-        // - disabled: Disable the live validating.
-        // The error messages are only shown after the form is submitted
-        // - submitted: The live validating is enabled after the form is submitted
+        feedbackIcons: null,
+        submitButtons: 'button[type="submit"]',
+        submitHandler: null,
         live: 'enabled',
+        fields: ...
+    });
+});
+```
+
+```message```: The default error message for all fields. You can specify the error message for any fields
+
+```columns```: The number of grid columns. Change it if you use custom grid with different number of columns
+
+```feedbackIcons```: Show valid/invalid/validating icons based on the field validity.
+
+This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
+Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
+In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
 
-        // The fields which need to be validated
-        fields: {
+It supports [Glyphicons](http://getbootstrap.com/components/#glyphicons) (included in Bootstrap):
+
+```javascript
+feedbackIcons: {
+    valid: 'glyphicon glyphicon-ok',
+    invalid: 'glyphicon glyphicon-remove',
+    validating: 'glyphicon glyphicon-refresh'
+}
+```
+
+and [FontAwesome](http://fontawesome.io/icons) icons (don't forget to insert FontAwesome CSS in your ```head```):
+
+```javascript
+feedbackIcons: {
+    valid: 'fa fa-check',
+    invalid: 'fa fa-times',
+    validating: 'fa fa-refresh'
+}
+```
+
+```submitButtons```: The submit buttons selector. These buttons will be disabled when the form input are invalid
+
+```submitHandler```: Custom submit handler.
+
+The handler has three arguments:
+
+- ```validator``` is the instance of BootstrapValidator
+- ```form``` is jQuery object representing the current form
+- ```submitButton``` is jQuery object representing the submit button which is clicked
+
+By default, ```submitHandler``` is ```null```
+
+```live```: Live validating. Can be one of 3 values:
+
+- ```enabled```: The plugin validates fields as soon as they are changed
+- ```disabled```: Disable the live validating. The error messages are only shown after the form is submitted
+- ```submitted```: The live validating is enabled after the form is submitted
+
+```fields```: The fields which need to be validated
+
+```javascript
+fields: {
+    ...
+    // The field name
+    // It is value of the name attribute
+    <fieldName>: {
+        // The default error message for this field
+        message: ...,
+
+        // Array of validators
+        validators: {
             ...
-            // The field name
-            // It is value of the name attribute
-            <fieldName>: {
-                // The default error message for this field
-                message: ...,
-
-                // Array of validators
-                validators: {
-                    ...
-                    <validatorName>: <validatorOptions>
-                    ...
-                }
-            }
+            <validatorName>: <validatorOptions>
             ...
         }
-    });
-});
+    }
+    ...
+}
 ```
 
 The ```<validatorName>``` can be the name of the built-in validator which are described in the [Validators](#validators) section

+ 8 - 1
demo/index.html

@@ -6,6 +6,9 @@
     <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
     <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>
 
+    <!-- Include the FontAwesome CSS if you want to use feedback icons provided by FontAwesome -->
+    <!--<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.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>
@@ -179,7 +182,11 @@ $(document).ready(function() {
     $('#defaultForm').bootstrapValidator({
 //        live: 'disabled',
         message: 'This value is not valid',
-        feedbackIcons: true,
+        feedbackIcons: {
+            valid: 'glyphicon glyphicon-ok',
+            invalid: 'glyphicon glyphicon-remove',
+            validating: 'glyphicon glyphicon-refresh'
+        },
         fields: {
             username: {
                 message: 'The username is not valid',

+ 20 - 25
dist/js/bootstrapValidator.js

@@ -40,11 +40,25 @@
         // Change it if you use custom grid with different number of columns
         columns: 12,
 
-        // Shows ok/error icons based on the field validity.
+        // Shows ok/error/loading icons based on the field validity.
         // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
         // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
         // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
-        feedbackIcons: false,
+        //
+        // Examples:
+        // - Use Glyphicons icons:
+        //  feedbackIcons: {
+        //      valid: 'glyphicon glyphicon-ok',
+        //      invalid: 'glyphicon glyphicon-remove',
+        //      validating: 'glyphicon glyphicon-refresh'
+        //  }
+        // - Use FontAwesome icons:
+        //  feedbackIcons: {
+        //      valid: 'fa fa-check',
+        //      invalid: 'fa fa-times',
+        //      validating: 'fa fa-refresh'
+        //  }
+        feedbackIcons: null,
 
         // The submit buttons selector
         // These buttons will be disabled to prevent the valid form from multiple submissions
@@ -179,7 +193,7 @@
             // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
             if (this.options.feedbackIcons) {
                 $parent.addClass('has-feedback');
-                $('<span/>').css('display', 'none').addClass('glyphicon form-control-feedback').insertAfter($(fields[fields.length - 1]));
+                $('<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
@@ -382,7 +396,7 @@
 
                     if (this.options.feedbackIcons) {
                         // Show "loading" icon
-                        $parent.find('.form-control-feedback').removeClass('glyphicon-ok').removeClass('glyphicon-remove').addClass('glyphicon-refresh').show();
+                        $parent.find('.form-control-feedback').removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
                     }
                     break;
 
@@ -397,7 +411,7 @@
 
                     if (this.options.feedbackIcons) {
                         // Show "error" icon
-                        $parent.find('.form-control-feedback').removeClass('glyphicon-ok').removeClass('glyphicon-refresh').addClass('glyphicon-remove').show();
+                        $parent.find('.form-control-feedback').removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
                     }
                     break;
 
@@ -418,7 +432,7 @@
                         $parent.removeClass('has-error').addClass('has-success');
                         // Show the "ok" icon
                         if (this.options.feedbackIcons) {
-                            $parent.find('.form-control-feedback').removeClass('glyphicon-remove').removeClass('glyphicon-refresh').addClass('glyphicon-ok').show();
+                            $parent.find('.form-control-feedback').removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.valid).show();
                         }
                     }
                     break;
@@ -665,25 +679,6 @@
     }
 }(window.jQuery));
 ;(function($) {
-    $.fn.bootstrapValidator.validators.dkZipCode = {
-        /**
-         * Return true if and only if the input value is a valid DK zip code
-         *
-         * @param {BootstrapValidator} validator The validator plugin instance
-         * @param {jQuery} $field Field element
-         * @param {Object} options
-         * @returns {Boolean}
-         */
-        validate: function(validateInstance, $field, options) {
-            var value = $field.val();
-            if (value == '') {
-                return true;
-            }
-            return /^(DK(-|\s)?)?\d{4}$/i.test(value);
-        }
-    };
-}(window.jQuery));
-;(function($) {
     $.fn.bootstrapValidator.validators.emailAddress = {
         /**
          * Return true if and only if the input value is a valid email address

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


+ 20 - 6
src/js/bootstrapValidator.js

@@ -39,11 +39,25 @@
         // Change it if you use custom grid with different number of columns
         columns: 12,
 
-        // Shows ok/error icons based on the field validity.
+        // Shows ok/error/loading icons based on the field validity.
         // This feature requires Bootstrap v3.1.0 or later (http://getbootstrap.com/css/#forms-control-validation).
         // Since Bootstrap doesn't provide any methods to know its version, this option cannot be on/off automatically.
         // In other word, to use this feature you have to upgrade your Bootstrap to v3.1.0 or later.
-        feedbackIcons: false,
+        //
+        // Examples:
+        // - Use Glyphicons icons:
+        //  feedbackIcons: {
+        //      valid: 'glyphicon glyphicon-ok',
+        //      invalid: 'glyphicon glyphicon-remove',
+        //      validating: 'glyphicon glyphicon-refresh'
+        //  }
+        // - Use FontAwesome icons:
+        //  feedbackIcons: {
+        //      valid: 'fa fa-check',
+        //      invalid: 'fa fa-times',
+        //      validating: 'fa fa-refresh'
+        //  }
+        feedbackIcons: null,
 
         // The submit buttons selector
         // These buttons will be disabled to prevent the valid form from multiple submissions
@@ -178,7 +192,7 @@
             // Available from Bootstrap 3.1 (http://getbootstrap.com/css/#forms-control-validation)
             if (this.options.feedbackIcons) {
                 $parent.addClass('has-feedback');
-                $('<span/>').css('display', 'none').addClass('glyphicon form-control-feedback').insertAfter($(fields[fields.length - 1]));
+                $('<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
@@ -381,7 +395,7 @@
 
                     if (this.options.feedbackIcons) {
                         // Show "loading" icon
-                        $parent.find('.form-control-feedback').removeClass('glyphicon-ok').removeClass('glyphicon-remove').addClass('glyphicon-refresh').show();
+                        $parent.find('.form-control-feedback').removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.invalid).addClass(this.options.feedbackIcons.validating).show();
                     }
                     break;
 
@@ -396,7 +410,7 @@
 
                     if (this.options.feedbackIcons) {
                         // Show "error" icon
-                        $parent.find('.form-control-feedback').removeClass('glyphicon-ok').removeClass('glyphicon-refresh').addClass('glyphicon-remove').show();
+                        $parent.find('.form-control-feedback').removeClass(this.options.feedbackIcons.valid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.invalid).show();
                     }
                     break;
 
@@ -417,7 +431,7 @@
                         $parent.removeClass('has-error').addClass('has-success');
                         // Show the "ok" icon
                         if (this.options.feedbackIcons) {
-                            $parent.find('.form-control-feedback').removeClass('glyphicon-remove').removeClass('glyphicon-refresh').addClass('glyphicon-ok').show();
+                            $parent.find('.form-control-feedback').removeClass(this.options.feedbackIcons.invalid).removeClass(this.options.feedbackIcons.validating).addClass(this.options.feedbackIcons.valid).show();
                         }
                     }
                     break;