ソースを参照

#238: The submit buttons are not sent

nghuuphuoc 11 年 前
コミット
7cb6045c2a

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 * [#232](https://github.com/nghuuphuoc/bootstrapvalidator/issues/232): Add ```id``` validator
 * [#242](https://github.com/nghuuphuoc/bootstrapvalidator/issues/242): Add ```separator``` option to the [```numeric``` validator](http://bootstrapvalidator.com/validators/numeric/)
 * Change default ```submitButtons``` to ```[type="submit"]``` to support ```input type="submit"```
+* [#238](https://github.com/nghuuphuoc/bootstrapvalidator/issues/238): The submit buttons are not sent
 * Fix the issue that the hidden fields generated by other plugins might not be validated
 * When parsing options from HTML attributes, don't add the field which hasn't validators. It improves fixes for [#191](https://github.com/nghuuphuoc/bootstrapvalidator/issues/191), [#223](https://github.com/nghuuphuoc/bootstrapvalidator/issues/223)
 

+ 1 - 1
demo/index.html

@@ -176,7 +176,7 @@
 
                         <div class="form-group">
                             <div class="col-lg-9 col-lg-offset-3">
-                                <button type="submit" class="btn btn-primary">Sign up</button>
+                                <button type="submit" class="btn btn-primary" name="signup" value="Sign up">Sign up</button>
                                 <button type="button" class="btn btn-info" id="validateBtn">Manual validate</button>
                                 <button type="button" class="btn btn-info" id="resetBtn">Reset form</button>
                             </div>

+ 55 - 44
dist/js/bootstrapValidator.js

@@ -172,56 +172,67 @@
 					that._submitIfValid = true;
                 })
                 // Find all fields which have either "name" or "data-bv-field" attribute
-                .find('[name], [data-bv-field]').each(function() {
-                    var $field = $(this);
-                    if (that._isExcluded($field)) {
-                        return;
-                    }
+                .find('[name], [data-bv-field]')
+                    .each(function() {
+                        var $field = $(this);
+                        if (that._isExcluded($field)) {
+                            return;
+                        }
 
-                    var field      = $field.attr('name') || $field.attr('data-bv-field'),
-                        validators = {};
-                    for (v in $.fn.bootstrapValidator.validators) {
-                        validator  = $.fn.bootstrapValidator.validators[v];
-                        enabled    = $field.attr('data-bv-' + v.toLowerCase()) + '';
-                        html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
-
-                        if ((html5Attrs && enabled != 'false')
-                            || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
-                        {
-                            // Try to parse the options via attributes
-                            validator.html5Attributes = validator.html5Attributes || { message: 'message' };
-                            validators[v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, validators[v]);
-
-                            for (html5AttrName in validator.html5Attributes) {
-                                optionName  = validator.html5Attributes[html5AttrName];
-                                optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
-                                if (optionValue) {
-                                    if ('true' == optionValue) {
-                                        optionValue = true;
-                                    } else if ('false' == optionValue) {
-                                        optionValue = false;
+                        var field      = $field.attr('name') || $field.attr('data-bv-field'),
+                            validators = {};
+                        for (v in $.fn.bootstrapValidator.validators) {
+                            validator  = $.fn.bootstrapValidator.validators[v];
+                            enabled    = $field.attr('data-bv-' + v.toLowerCase()) + '';
+                            html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
+
+                            if ((html5Attrs && enabled != 'false')
+                                || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
+                            {
+                                // Try to parse the options via attributes
+                                validator.html5Attributes = validator.html5Attributes || { message: 'message' };
+                                validators[v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, validators[v]);
+
+                                for (html5AttrName in validator.html5Attributes) {
+                                    optionName  = validator.html5Attributes[html5AttrName];
+                                    optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
+                                    if (optionValue) {
+                                        if ('true' == optionValue) {
+                                            optionValue = true;
+                                        } else if ('false' == optionValue) {
+                                            optionValue = false;
+                                        }
+                                        validators[v][optionName] = optionValue;
                                     }
-                                    validators[v][optionName] = optionValue;
                                 }
                             }
                         }
-                    }
-
-                    var opts = {
-                        trigger:    $field.attr('data-bv-trigger'),
-                        message:    $field.attr('data-bv-message'),
-                        container:  $field.attr('data-bv-container'),
-                        selector:   $field.attr('data-bv-selector'),
-                        threshold:  $field.attr('data-bv-threshold'),
-                        validators: validators
-                    };
 
-                    // Check if there is any validators set using HTML attributes
-                    if (!$.isEmptyObject(opts.validators) && !$.isEmptyObject(opts)) {
-                        $field.attr('data-bv-field', field);
-                        options.fields[field] = $.extend({}, opts, options.fields[field]);
-                    }
-                });
+                        var opts = {
+                            trigger:    $field.attr('data-bv-trigger'),
+                            message:    $field.attr('data-bv-message'),
+                            container:  $field.attr('data-bv-container'),
+                            selector:   $field.attr('data-bv-selector'),
+                            threshold:  $field.attr('data-bv-threshold'),
+                            validators: validators
+                        };
+
+                        // Check if there is any validators set using HTML attributes
+                        if (!$.isEmptyObject(opts.validators) && !$.isEmptyObject(opts)) {
+                            $field.attr('data-bv-field', field);
+                            options.fields[field] = $.extend({}, opts, options.fields[field]);
+                        }
+                    })
+                    .end()
+                // Create hidden inputs to send the submit buttons
+                .find(this.options.submitButtons)
+                    .each(function() {
+                        $('<input/>')
+                            .attr('type', 'hidden')
+                            .attr('name', $(this).attr('name'))
+                            .attr('value', $(this).val())
+                            .appendTo(that.$form);
+                    });
 
             this.options = $.extend(true, this.options, options);
             if ('string' == typeof this.options.excluded) {

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


+ 55 - 44
src/js/bootstrapValidator.js

@@ -171,56 +171,67 @@
 					that._submitIfValid = true;
                 })
                 // Find all fields which have either "name" or "data-bv-field" attribute
-                .find('[name], [data-bv-field]').each(function() {
-                    var $field = $(this);
-                    if (that._isExcluded($field)) {
-                        return;
-                    }
+                .find('[name], [data-bv-field]')
+                    .each(function() {
+                        var $field = $(this);
+                        if (that._isExcluded($field)) {
+                            return;
+                        }
 
-                    var field      = $field.attr('name') || $field.attr('data-bv-field'),
-                        validators = {};
-                    for (v in $.fn.bootstrapValidator.validators) {
-                        validator  = $.fn.bootstrapValidator.validators[v];
-                        enabled    = $field.attr('data-bv-' + v.toLowerCase()) + '';
-                        html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
-
-                        if ((html5Attrs && enabled != 'false')
-                            || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
-                        {
-                            // Try to parse the options via attributes
-                            validator.html5Attributes = validator.html5Attributes || { message: 'message' };
-                            validators[v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, validators[v]);
-
-                            for (html5AttrName in validator.html5Attributes) {
-                                optionName  = validator.html5Attributes[html5AttrName];
-                                optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
-                                if (optionValue) {
-                                    if ('true' == optionValue) {
-                                        optionValue = true;
-                                    } else if ('false' == optionValue) {
-                                        optionValue = false;
+                        var field      = $field.attr('name') || $field.attr('data-bv-field'),
+                            validators = {};
+                        for (v in $.fn.bootstrapValidator.validators) {
+                            validator  = $.fn.bootstrapValidator.validators[v];
+                            enabled    = $field.attr('data-bv-' + v.toLowerCase()) + '';
+                            html5Attrs = ('function' == typeof validator.enableByHtml5) ? validator.enableByHtml5($(this)) : null;
+
+                            if ((html5Attrs && enabled != 'false')
+                                || (html5Attrs !== true && ('' == enabled || 'true' == enabled)))
+                            {
+                                // Try to parse the options via attributes
+                                validator.html5Attributes = validator.html5Attributes || { message: 'message' };
+                                validators[v] = $.extend({}, html5Attrs == true ? {} : html5Attrs, validators[v]);
+
+                                for (html5AttrName in validator.html5Attributes) {
+                                    optionName  = validator.html5Attributes[html5AttrName];
+                                    optionValue = $field.attr('data-bv-' + v.toLowerCase() + '-' + html5AttrName);
+                                    if (optionValue) {
+                                        if ('true' == optionValue) {
+                                            optionValue = true;
+                                        } else if ('false' == optionValue) {
+                                            optionValue = false;
+                                        }
+                                        validators[v][optionName] = optionValue;
                                     }
-                                    validators[v][optionName] = optionValue;
                                 }
                             }
                         }
-                    }
-
-                    var opts = {
-                        trigger:    $field.attr('data-bv-trigger'),
-                        message:    $field.attr('data-bv-message'),
-                        container:  $field.attr('data-bv-container'),
-                        selector:   $field.attr('data-bv-selector'),
-                        threshold:  $field.attr('data-bv-threshold'),
-                        validators: validators
-                    };
 
-                    // Check if there is any validators set using HTML attributes
-                    if (!$.isEmptyObject(opts.validators) && !$.isEmptyObject(opts)) {
-                        $field.attr('data-bv-field', field);
-                        options.fields[field] = $.extend({}, opts, options.fields[field]);
-                    }
-                });
+                        var opts = {
+                            trigger:    $field.attr('data-bv-trigger'),
+                            message:    $field.attr('data-bv-message'),
+                            container:  $field.attr('data-bv-container'),
+                            selector:   $field.attr('data-bv-selector'),
+                            threshold:  $field.attr('data-bv-threshold'),
+                            validators: validators
+                        };
+
+                        // Check if there is any validators set using HTML attributes
+                        if (!$.isEmptyObject(opts.validators) && !$.isEmptyObject(opts)) {
+                            $field.attr('data-bv-field', field);
+                            options.fields[field] = $.extend({}, opts, options.fields[field]);
+                        }
+                    })
+                    .end()
+                // Create hidden inputs to send the submit buttons
+                .find(this.options.submitButtons)
+                    .each(function() {
+                        $('<input/>')
+                            .attr('type', 'hidden')
+                            .attr('name', $(this).attr('name'))
+                            .attr('value', $(this).val())
+                            .appendTo(that.$form);
+                    });
 
             this.options = $.extend(true, this.options, options);
             if ('string' == typeof this.options.excluded) {