ソースを参照

#732: Fix the issue when removing the radio or checkbox field

Phuoc Nguyen 11 年 前
コミット
7407ba6813

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@ __Improvements__
 
 __Bug Fixes__
 * [#687](https://github.com/nghuuphuoc/bootstrapvalidator/issues/687), [#711](https://github.com/nghuuphuoc/bootstrapvalidator/pull/711): Keep disabled validators VALID, thanks to [@talberti](https://github.com/talberti)
+* [#732](https://github.com/nghuuphuoc/bootstrapvalidator/issues/732): Fix the issue when removing the radio or checkbox field
 
 __Document__
 * [#709](https://github.com/nghuuphuoc/bootstrapvalidator/issues/709), [#715](https://github.com/nghuuphuoc/bootstrapvalidator/pull/715): Add [Bootstrap Select](http://bootstrapvalidator.com/examples/bootstrap-select/) and [Select2](http://bootstrapvalidator.com/examples/select2/) examples, thanks to [@Arkni](https://github.com/Arkni)

+ 181 - 0
demo/test.html

@@ -0,0 +1,181 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>BootstrapValidator demo</title>
+
+    <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
+    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.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>Dynamic fields</h2>
+                </div>
+
+                <div class="form-group hide" id="radioTemplate">
+                    <div class="col-md-offset-3 col-md-5">
+                        <label>
+                            <input type="radio" name="elementName" />
+                            Yes
+                        </label>
+                        <label>
+                            <input type="radio" name="elementName" />
+                            No
+                        </label>
+                    </div>
+                    <div class="col-md-4">
+                        <button type="button" class="btn btn-default removeButton">
+                            <i class="fa fa-minus"></i>
+                        </button>
+                    </div>
+                </div>
+
+                <div class="form-group hide" id="textTemplate">
+                    <div class="col-md-offset-3 col-md-5">
+                        <input type="text" name="elementName" class="form-control" />
+                    </div>
+                    <div class="col-md-4">
+                        <button type="button" class="btn btn-default removeButton">
+                            <i class="fa fa-minus"></i>
+                        </button>
+                    </div>
+                </div>
+
+                <div class="container">
+                    <div class="row">
+                        <div class="col-sm-12">
+
+                        <select id="addType" class="form-control">
+                            <option value="#textTemplate">Add input[type='text']</option>
+                            <option value="#radioTemplate">Add input[type='radio']</option>
+                        </select>
+
+                        <button type="button" class="btn btn-default addButton" id="addNewElement">
+                                <i class="fa fa-plus"></i>
+                            </button>
+
+                <form id="surveyForm" method="post" class="form-horizontal">
+
+                    <div class="form-group" id="submitFormGroup">
+                        <div class="col-md-5 col-md-offset-3">
+                            <button type="submit" class="btn btn-default">Validate</button>
+                        </div>
+                    </div>
+                </form>
+
+                        </div>
+                    </div>
+                </div>
+            </div>
+        </section>
+        <!-- :form -->
+    </div>
+</div>
+
+<script type="text/javascript">
+$(document).ready(function() {
+    // The maximum number of options
+    var MAX_OPTIONS = 5;
+    var ind = 0;
+
+    $('#addNewElement').click(function () {
+
+        // determine which template to add
+        var $template = $($('#addType').val()),
+            $clone    = $template
+                            .clone()
+                            .removeClass('hide')
+                            .removeAttr('id')
+                            .attr('data-index', ind),
+            $option   = $clone.find('[name="elementName"]').attr('name', 'field' + ind);
+
+            $clone.insertBefore($('#submitFormGroup'));
+
+            // Add new field
+            $('#surveyForm').bootstrapValidator('addField', $option);
+
+        ind++;
+
+    });
+
+    $('#surveyForm')
+        .bootstrapValidator({
+            feedbackIcons: {
+                valid: 'glyphicon glyphicon-ok',
+                invalid: 'glyphicon glyphicon-remove',
+                validating: 'glyphicon glyphicon-refresh'
+            },
+            fields: {
+                question: {
+                    validators: {
+                        notEmpty: {
+                            message: 'The question required and cannot be empty'
+                        }
+                    }
+                },
+                'option[]': {
+                    validators: {
+                        notEmpty: {
+                            message: 'The option required and cannot be empty'
+                        },
+                        stringLength: {
+                            max: 100,
+                            message: 'The option must be less than 100 characters long'
+                        }
+                    }
+                }
+            }
+        })
+
+        // Remove button click handler
+        .on('click', '.removeButton', function() {
+
+            var $row    = $(this).parents('.form-group'),
+                $ind = $row.attr('data-index'),
+                $option = $row.find('[name="field' + $ind + '"]');
+
+            $('#surveyForm').bootstrapValidator('removeField', $option.eq(0).attr('name'));
+
+            // Remove element containing the option
+            $row.remove();
+
+            // Remove field
+
+            ind--;
+
+        })
+
+        // Called after adding new field
+        .on('added.field.bv', function(e, data) {
+            // data.field   --> The field name
+            // data.element --> The new field element
+            // data.options --> The new field options
+
+            if (data.field === 'option[]') {
+                if ($('#surveyForm').find(':visible[name="option[]"]').length >= MAX_OPTIONS) {
+                    $('#surveyForm').find('.addButton').attr('disabled', 'disabled');
+                }
+            }
+        })
+
+        // Called after removing the field
+        .on('removed.field.bv', function(e, data) {
+           if (data.field === 'option[]') {
+                if ($('#surveyForm').find(':visible[name="option[]"]').length < MAX_OPTIONS) {
+                    $('#surveyForm').find('.addButton').removeAttr('disabled');
+                }
+            }
+        });
+});
+</script>
+</body>
+</html>

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.2-dev, built on 2014-08-27 5:03:48 PM
+ * @version     v0.5.2-dev, built on 2014-08-28 9:57:44 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 6 - 5
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.2-dev, built on 2014-08-27 5:03:48 PM
+ * @version     v0.5.2-dev, built on 2014-08-28 9:57:44 AM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -243,15 +243,16 @@
                     break;
             }
 
-            if (this.options.fields[field] === null || this.options.fields[field].validators === null) {
-                return;
-            }
-
             // We don't need to validate non-existing fields
             if (fields.length === 0) {
                 delete this.options.fields[field];
                 return;
             }
+
+            if (this.options.fields[field] === null || this.options.fields[field].validators === null) {
+                return;
+            }
+
             var validatorName;
             for (validatorName in this.options.fields[field].validators) {
                 if (!$.fn.bootstrapValidator.validators[validatorName]) {

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


+ 5 - 4
src/js/bootstrapValidator.js

@@ -242,15 +242,16 @@
                     break;
             }
 
-            if (this.options.fields[field] === null || this.options.fields[field].validators === null) {
-                return;
-            }
-
             // We don't need to validate non-existing fields
             if (fields.length === 0) {
                 delete this.options.fields[field];
                 return;
             }
+
+            if (this.options.fields[field] === null || this.options.fields[field].validators === null) {
+                return;
+            }
+
             var validatorName;
             for (validatorName in this.options.fields[field].validators) {
                 if (!$.fn.bootstrapValidator.validators[validatorName]) {