ソースを参照

(#40) Fix the issue when the form label doesn't have class

nghuuphuoc 12 年 前
コミット
f6c0a851a5

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,7 @@
 * (#31) ```remote``` validator: Allow to set additional data to remote URL
 * (#34) Avoid from calling form submit recursively
 * (#39) Validate existing fields only
+* (#40) Fix the issue when the form label doesn't have class
 
 ##v0.2.1 (2013-11-08)
 

+ 0 - 2
demo/index.html

@@ -49,14 +49,12 @@
                             </div>
                         </div>
 
-                        <!--
                         <div class="form-group">
                             <label class="col-lg-3 control-label" id="captchaOperation"></label>
                             <div class="col-lg-2">
                                 <input type="text" class="form-control" name="captcha" />
                             </div>
                         </div>
-                        -->
 
                         <div class="form-group">
                             <div class="col-lg-9 col-lg-offset-3">

+ 113 - 0
demo/labelWithoutClass.html

@@ -0,0 +1,113 @@
+<!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" style="margin-top: 50px;">
+        <div class="row">
+            <div class="col-lg-4 col-lg-offset-4">
+                <div class="panel panel-default">
+                    <div class="panel-heading">
+                        <h3 class="panel-title">Sign up</h3>
+                    </div>
+
+                    <div class="panel-body">
+                        <form id="defaultForm" method="post">
+                            <div class="form-group">
+                                <label>Username</label>
+                                <input type="text" class="form-control" name="username" placeholder="Username" />
+                            </div>
+
+                            <div class="form-group">
+                                <label>Email</label>
+                                <input type="text" class="form-control" name="email" placeholder="Email" />
+                            </div>
+
+                            <div class="form-group">
+                                <label>Password</label>
+                                <input type="password" class="form-control" name="password" placeholder="Password" />
+                            </div>
+
+                            <div class="form-group">
+                                <label>Retype password</label>
+                                <input type="password" class="form-control" name="confirmPassword" placeholder="Retype password" />
+                            </div>
+
+                            <div class="form-group">
+                                <button type="submit" class="btn btn-primary">Sign up</button>
+                            </div>
+                        </form>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+
+<script type="text/javascript">
+$(document).ready(function() {
+    $('#defaultForm').bootstrapValidator({
+        message: 'This value is not valid',
+        fields: {
+            username: {
+                message: 'The username is not valid',
+                validators: {
+                    notEmpty: {
+                        message: 'The username is required and can\'t 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'
+                    }
+                }
+            },
+            email: {
+                validators: {
+                    notEmpty: {
+                        message: 'The email address is required and can\'t be empty'
+                    },
+                    emailAddress: {
+                        message: 'The input is not a valid email address'
+                    }
+                }
+            },
+            password: {
+                validators: {
+                    notEmpty: {
+                        message: 'The password is required and can\'t be empty'
+                    },
+                    identical: {
+                        field: 'confirmPassword',
+                        message: 'The password and its confirm are not the same'
+                    }
+                }
+            },
+            confirmPassword: {
+                validators: {
+                    notEmpty: {
+                        message: 'The confirm password is required and can\'t be empty'
+                    },
+                    identical: {
+                        field: 'password',
+                        message: 'The password and its confirm are not the same'
+                    }
+                }
+            }
+        }
+    });
+});
+</script>
+</body>
+</html>

+ 11 - 8
dist/js/bootstrapValidator.js

@@ -171,16 +171,19 @@
                 // Then set offset to the help block element
                 var label, cssClasses, offset, size;
                 if (label = $parent.find('label').get(0)) {
-                    cssClasses = $(label).attr('class').split(' ');
-                    for (var i = 0; i < cssClasses.length; i++) {
-                        if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i])) {
-                            offset = cssClasses[i].substr(7);
-                            size   = cssClasses[i].substr(4, 2);
-                            break;
+                    // The default Bootstrap form don't require class for label (http://getbootstrap.com/css/#forms)
+                    if (cssClasses = $(label).attr('class')) {
+                        cssClasses = cssClasses.split(' ');
+                        for (var i = 0; i < cssClasses.length; i++) {
+                            if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i])) {
+                                offset = cssClasses[i].substr(7);
+                                size   = cssClasses[i].substr(4, 2);
+                                break;
+                            }
                         }
                     }
-                } else {
-                    cssClasses = $parent.children().eq(0).attr('class').split(' ');
+                } else if (cssClasses = $parent.children().eq(0).attr('class')) {
+                    cssClasses = cssClasses.split(' ');
                     for (var i = 0; i < cssClasses.length; i++) {
                         if (/^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
                             offset = cssClasses[i].substr(14);

ファイルの差分が大きいため隠しています
+ 1 - 1
dist/js/bootstrapValidator.min.js


+ 11 - 8
src/js/bootstrapValidator.js

@@ -171,16 +171,19 @@
                 // Then set offset to the help block element
                 var label, cssClasses, offset, size;
                 if (label = $parent.find('label').get(0)) {
-                    cssClasses = $(label).attr('class').split(' ');
-                    for (var i = 0; i < cssClasses.length; i++) {
-                        if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i])) {
-                            offset = cssClasses[i].substr(7);
-                            size   = cssClasses[i].substr(4, 2);
-                            break;
+                    // The default Bootstrap form don't require class for label (http://getbootstrap.com/css/#forms)
+                    if (cssClasses = $(label).attr('class')) {
+                        cssClasses = cssClasses.split(' ');
+                        for (var i = 0; i < cssClasses.length; i++) {
+                            if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i])) {
+                                offset = cssClasses[i].substr(7);
+                                size   = cssClasses[i].substr(4, 2);
+                                break;
+                            }
                         }
                     }
-                } else {
-                    cssClasses = $parent.children().eq(0).attr('class').split(' ');
+                } else if (cssClasses = $parent.children().eq(0).attr('class')) {
+                    cssClasses = cssClasses.split(' ');
                     for (var i = 0; i < cssClasses.length; i++) {
                         if (/^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
                             offset = cssClasses[i].substr(14);