Browse Source

#356: Add group option

nghuuphuoc 11 years ago
parent
commit
dac1479f6c
5 changed files with 186 additions and 17 deletions
  1. 2 1
      CHANGELOG.md
  2. 144 0
      demo/group.html
  3. 19 7
      dist/js/bootstrapValidator.js
  4. 2 2
      dist/js/bootstrapValidator.min.js
  5. 19 7
      src/js/bootstrapValidator.js

+ 2 - 1
CHANGELOG.md

@@ -19,6 +19,7 @@ __New Features__
 * [#324](https://github.com/nghuuphuoc/bootstrapvalidator/issues/324): Add ```success.validator.bv``` and ```error.validator.bv``` events triggered after a validator completes
 * [#332](https://github.com/nghuuphuoc/bootstrapvalidator/pull/332): Add UK phone number support for the [```phone``` validator](http://bootstrapvalidator.com/validators/phone/), thanks to [@aca02djr](https://github.com/aca02djr)
 * [#336](https://github.com/nghuuphuoc/bootstrapvalidator/issues/336): Add ```$field``` instance to the [callback validator](http://bootstrapvalidator.com/validators/callback/)
+* [#356](https://github.com/nghuuphuoc/bootstrapvalidator/issues/356): Add ```group``` option
 * [#374](https://github.com/nghuuphuoc/bootstrapvalidator/pull/374): Add Singapore postal code to the [zipCode validator](http://bootstrapvalidator.com/validators/zipCode/), thanks to [@thisisclement](https://github.com/thisisclement)
 
 __Changes__
@@ -29,7 +30,7 @@ __Improvements__
 * [#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)
 * [#262](https://github.com/nghuuphuoc/bootstrapvalidator/issues/262): Improve the [```updateStatus()``` method](http://bootstrapvalidator.com/api/#update-status). The plugin now doesn't show the errors, feedback icons of given field if there are uncompleted validators
 * [#274](https://github.com/nghuuphuoc/bootstrapvalidator/pull/274): Fix feedback icons in ```input-group```, thanks to [@tiagofontella](https://github.com/tiagofontella)
-* [#287](https://github.com/nghuuphuoc/bootstrapvalidator/issues/287): Only send the submit button which is clicked. It's an enhancement for [#238](https://github.com/nghuuphuoc/bootstrapvalidator/issues/238)
+* [#287](https://github.com/nghuuphuoc/bootstrapvalidator/issues/287), [#291](https://github.com/nghuuphuoc/bootstrapvalidator/issues/291): Only send the submit button which is clicked. It's an enhancement for [#238](https://github.com/nghuuphuoc/bootstrapvalidator/issues/238)
 * [#297](https://github.com/nghuuphuoc/bootstrapvalidator/issues/297): Disable feedback icons for particular fields
 * [#348](https://github.com/nghuuphuoc/bootstrapvalidator/issues/348): The [uri validator](http://bootstrapvalidator.com/validators/uri/) now provides an option to support private/local network address
 * [#364](https://github.com/nghuuphuoc/bootstrapvalidator/issues/364): Clicking the feedback icon also effect to the checkbox, radio fields

+ 144 - 0
demo/group.html

@@ -0,0 +1,144 @@
+<!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">
+            <div class="col-lg-8 col-lg-offset-2">
+                <div class="page-header">
+                    <h2>group option</h2>
+                </div>
+
+                <form id="form" method="post" class="form-horizontal" action="target.php">
+                    <div class="form-group">
+                        <div class="group">
+                            <label class="col-sm-2 control-label">First name</label>
+                            <div class="col-sm-4">
+                                <input type="text" class="form-control" name="firstName" />
+                            </div>
+                        </div>
+                        <div class="group">
+                            <label class="col-sm-2 control-label">Last name</label>
+                            <div class="col-sm-4">
+                                <input type="text" class="form-control" name="lastName" data-bv-group=".group" />
+                            </div>
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-sm-2 control-label">Username</label>
+                        <div class="col-sm-5">
+                            <input type="text" class="form-control" name="username" />
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-sm-2 control-label">Email address</label>
+                        <div class="col-sm-5">
+                            <input type="text" class="form-control" name="email" />
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <label class="col-sm-2 control-label">Password</label>
+                        <div class="col-sm-5">
+                            <input type="password" class="form-control" name="password" />
+                        </div>
+                    </div>
+
+                    <div class="form-group">
+                        <div class="col-sm-10 col-sm-offset-2">
+                            <button type="submit" class="btn btn-primary">Submit</button>
+                        </div>
+                    </div>
+                </form>
+            </div>
+        </div>
+    </div>
+
+<script type="text/javascript">
+$(document).ready(function() {
+    $('#form').bootstrapValidator({
+        message: 'This value is not valid',
+        feedbackIcons: {
+            valid: 'glyphicon glyphicon-ok',
+            invalid: 'glyphicon glyphicon-remove',
+            validating: 'glyphicon glyphicon-refresh'
+        },
+        //group: '.form-group',
+        fields: {
+            firstName: {
+                group: '.group',
+                validators: {
+                    notEmpty: {
+                        message: 'The first name is required and cannot be empty'
+                    }
+                }
+            },
+            lastName: {
+                // The "group" option can be set via HTML attribute
+                // <input type="text" class="form-control" name="lastName" data-bv-group=".group" />
+                validators: {
+                    notEmpty: {
+                        message: 'The last name is required and cannot be empty'
+                    }
+                }
+            },
+            username: {
+                message: 'The username is not valid',
+                validators: {
+                    notEmpty: {
+                        message: 'The username is required and cannot be empty'
+                    },
+                    stringLength: {
+                        min: 6,
+                        max: 30,
+                        message: 'The username must be more than 6 and less than 30 characters long'
+                    },
+                    regexp: {
+                        regexp: /^[a-zA-Z0-9_\.]+$/,
+                        message: 'The username can only consist of alphabetical, number, dot and underscore'
+                    },
+                    different: {
+                        field: 'password',
+                        message: 'The username and password cannot be the same as each other'
+                    }
+                }
+            },
+            email: {
+                validators: {
+                    emailAddress: {
+                        message: 'The input is not a valid email address'
+                    }
+                }
+            },
+            password: {
+                validators: {
+                    notEmpty: {
+                        message: 'The password is required and cannot be empty'
+                    },
+                    identical: {
+                        field: 'confirmPassword',
+                        message: 'The password and its confirm are not the same'
+                    },
+                    different: {
+                        field: 'username',
+                        message: 'The password cannot be the same as username'
+                    }
+                }
+            }
+        }
+    });
+});
+</script>
+</body>
+</html>

+ 19 - 7
dist/js/bootstrapValidator.js

@@ -54,6 +54,11 @@
         // Default invalid message
         message: 'This value is not valid',
 
+        // The CSS selector for indicating the element consists the field
+        // By default, each field is placed inside the <div class="form-group"></div>
+        // You should adjust this option if your form group consists of many fields which not all of them need to be validated
+        group: '.form-group',
+
         // The error messages container
         // It can be:
         // * 'tooltip' if you want to use Bootstrap tooltip to show error messages
@@ -153,6 +158,7 @@
                     trigger:        this.$form.attr('data-bv-trigger'),
                     message:        this.$form.attr('data-bv-message'),
                     container:      this.$form.attr('data-bv-container'),
+                    group:          this.$form.attr('data-bv-group'),
                     submitButtons:  this.$form.attr('data-bv-submitbuttons'),
                     threshold:      this.$form.attr('data-bv-threshold'),
                     live:           this.$form.attr('data-bv-live'),
@@ -253,6 +259,7 @@
                     trigger:       $field.attr('data-bv-trigger'),
                     message:       $field.attr('data-bv-message'),
                     container:     $field.attr('data-bv-container'),
+                    group:         $field.attr('data-bv-group'),
                     selector:      $field.attr('data-bv-selector'),
                     threshold:     $field.attr('data-bv-threshold'),
                     validators:    validators
@@ -313,7 +320,8 @@
 
             for (var i = 0; i < total; i++) {
                 var $field    = fields.eq(i),
-                    $parent   = $field.parents('.form-group'),
+                    group     = this.options.fields[field].group || this.options.group,
+                    $parent   = $field.parents(group),
                     // Allow user to indicate where the error messages are shown
                     container = this.options.fields[field].container || this.options.container,
                     $message  = (container && container != 'tooltip' && container != 'popover') ? $(container) : this._getMessageContainer($field);
@@ -416,8 +424,10 @@
          * @returns {jQuery}
          */
         _getMessageContainer: function($field) {
-            var $parent = $field.parent();
-            if ($parent.hasClass('form-group')) {
+            var $parent = $field.parent(),
+                field   = $field.attr('data-bv-field'),
+                group   = this.options.fields[field].group || this.options.group;
+            if ($parent.is(group)) {
                 return $parent;
             }
 
@@ -799,11 +809,12 @@
 
             var that  = this,
                 type  = fields.attr('type'),
+                group = this.options.fields[field].group || this.options.group,
                 total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
 
             for (var i = 0; i < total; i++) {
                 var $field       = fields.eq(i),
-                    $parent      = $field.parents('.form-group'),
+                    $parent      = $field.parents(group),
                     $message     = $field.data('bv.messages'),
                     $allErrors   = $message.find('.help-block[data-bv-validator][data-bv-for="' + field + '"]'),
                     $errors      = validatorName ? $allErrors.filter('[data-bv-validator="' + validatorName + '"]') : $allErrors,
@@ -1273,7 +1284,8 @@
             var field, fields, $field, validator, $icon, container;
             for (field in this.options.fields) {
                 fields    = this.getFieldElements(field);
-                container = this.options.fields[field].container || this.options.container;
+                container = this.options.fields[field].container || this.options.container,
+                group     = this.options.fields[field].group || this.options.group;
                 for (var i = 0; i < fields.length; i++) {
                     $field = fields.eq(i);
                     $field
@@ -1283,7 +1295,7 @@
                             .end()
                         .removeData('bv.messages')
                         // Remove feedback classes
-                        .parents('.form-group')
+                        .parents(group)
                             .removeClass('has-feedback has-error has-success')
                             .end()
                         // Turn off events
@@ -1291,7 +1303,7 @@
                         .removeAttr('data-bv-field');
 
                     // Remove feedback icons, tooltip/popover container
-                    $icon = $field.parents('.form-group').find('i[data-bv-icon-for="' + field + '"]');
+                    $icon = $field.parents(group).find('i[data-bv-icon-for="' + field + '"]');
                     if ($icon) {
                         switch (container) {
                             case 'tooltip':

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


+ 19 - 7
src/js/bootstrapValidator.js

@@ -53,6 +53,11 @@
         // Default invalid message
         message: 'This value is not valid',
 
+        // The CSS selector for indicating the element consists the field
+        // By default, each field is placed inside the <div class="form-group"></div>
+        // You should adjust this option if your form group consists of many fields which not all of them need to be validated
+        group: '.form-group',
+
         // The error messages container
         // It can be:
         // * 'tooltip' if you want to use Bootstrap tooltip to show error messages
@@ -152,6 +157,7 @@
                     trigger:        this.$form.attr('data-bv-trigger'),
                     message:        this.$form.attr('data-bv-message'),
                     container:      this.$form.attr('data-bv-container'),
+                    group:          this.$form.attr('data-bv-group'),
                     submitButtons:  this.$form.attr('data-bv-submitbuttons'),
                     threshold:      this.$form.attr('data-bv-threshold'),
                     live:           this.$form.attr('data-bv-live'),
@@ -252,6 +258,7 @@
                     trigger:       $field.attr('data-bv-trigger'),
                     message:       $field.attr('data-bv-message'),
                     container:     $field.attr('data-bv-container'),
+                    group:         $field.attr('data-bv-group'),
                     selector:      $field.attr('data-bv-selector'),
                     threshold:     $field.attr('data-bv-threshold'),
                     validators:    validators
@@ -312,7 +319,8 @@
 
             for (var i = 0; i < total; i++) {
                 var $field    = fields.eq(i),
-                    $parent   = $field.parents('.form-group'),
+                    group     = this.options.fields[field].group || this.options.group,
+                    $parent   = $field.parents(group),
                     // Allow user to indicate where the error messages are shown
                     container = this.options.fields[field].container || this.options.container,
                     $message  = (container && container != 'tooltip' && container != 'popover') ? $(container) : this._getMessageContainer($field);
@@ -415,8 +423,10 @@
          * @returns {jQuery}
          */
         _getMessageContainer: function($field) {
-            var $parent = $field.parent();
-            if ($parent.hasClass('form-group')) {
+            var $parent = $field.parent(),
+                field   = $field.attr('data-bv-field'),
+                group   = this.options.fields[field].group || this.options.group;
+            if ($parent.is(group)) {
                 return $parent;
             }
 
@@ -798,11 +808,12 @@
 
             var that  = this,
                 type  = fields.attr('type'),
+                group = this.options.fields[field].group || this.options.group,
                 total = ('radio' == type || 'checkbox' == type) ? 1 : fields.length;
 
             for (var i = 0; i < total; i++) {
                 var $field       = fields.eq(i),
-                    $parent      = $field.parents('.form-group'),
+                    $parent      = $field.parents(group),
                     $message     = $field.data('bv.messages'),
                     $allErrors   = $message.find('.help-block[data-bv-validator][data-bv-for="' + field + '"]'),
                     $errors      = validatorName ? $allErrors.filter('[data-bv-validator="' + validatorName + '"]') : $allErrors,
@@ -1272,7 +1283,8 @@
             var field, fields, $field, validator, $icon, container;
             for (field in this.options.fields) {
                 fields    = this.getFieldElements(field);
-                container = this.options.fields[field].container || this.options.container;
+                container = this.options.fields[field].container || this.options.container,
+                group     = this.options.fields[field].group || this.options.group;
                 for (var i = 0; i < fields.length; i++) {
                     $field = fields.eq(i);
                     $field
@@ -1282,7 +1294,7 @@
                             .end()
                         .removeData('bv.messages')
                         // Remove feedback classes
-                        .parents('.form-group')
+                        .parents(group)
                             .removeClass('has-feedback has-error has-success')
                             .end()
                         // Turn off events
@@ -1290,7 +1302,7 @@
                         .removeAttr('data-bv-field');
 
                     // Remove feedback icons, tooltip/popover container
-                    $icon = $field.parents('.form-group').find('i[data-bv-icon-for="' + field + '"]');
+                    $icon = $field.parents(group).find('i[data-bv-icon-for="' + field + '"]');
                     if ($icon) {
                         switch (container) {
                             case 'tooltip':