Skip to content

Commit

Permalink
Merge pull request #1045 from jazzzz/double-submit
Browse files Browse the repository at this point in the history
Fixed double submit with defered validations
  • Loading branch information
nghuuphuoc committed Nov 3, 2014
2 parents bd15cae + 08a95e8 commit f240ea3
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/js/bootstrapValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,11 +839,13 @@ if (typeof jQuery === 'undefined') {
}
this.disableSubmitButtons(true);

this._submitIfValid = false;
for (var field in this.options.fields) {
this.validateField(field);
}

this._submit();
this._submitIfValid = true;

return this;
},
Expand Down
4 changes: 4 additions & 0 deletions test/spec/event.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var originalDefaultOptions = $.fn.bootstrapValidator.DEFAULT_OPTIONS;

TestSuite = $.extend({}, TestSuite, {
Event: {
onEmailValid: function(e, data) {
Expand Down Expand Up @@ -533,6 +535,7 @@ describe('event form trigger with events changed', function() {
afterEach(function() {
$.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
$('#eventForm2').bootstrapValidator('destroy').remove();
$.fn.bootstrapValidator.DEFAULT_OPTIONS = originalDefaultOptions;
});

it('triggers bv.form.success', function() {
Expand Down Expand Up @@ -609,6 +612,7 @@ describe('event field trigger with events changed', function() {
afterEach(function() {
$.fn.bootstrapValidator.DEFAULT_OPTIONS = defaultOptions;
$('#eventForm4').bootstrapValidator('destroy').remove();
$.fn.bootstrapValidator.DEFAULT_OPTIONS = originalDefaultOptions;
});

it('triggers success.field.bv', function() {
Expand Down
135 changes: 135 additions & 0 deletions test/spec/submit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
describe('submit', function() {

var submitted;

// Override the options
$.extend($.fn.bootstrapValidator.DEFAULT_OPTIONS, {
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
}
});

$.fn.bootstrapValidator.validators.fake_remote = {
validate: function(validator, $field, options) {
var dfd = new $.Deferred();
setTimeout(function() {
dfd.resolve($field, 'fake_remote', { valid: options.valid });
}, 0);
return dfd;
}
};

beforeEach(function() {
$([
'<form id="submitForm" class="form-horizontal" role="form">',
'<div class="form-group">',
'<input name="username" type="text" class="form-control" value="me" required />',
'</div>',
'<button id="sendButton" type="submit" class="btn btn-default">Send</button>',
'</form>'
].join('\n')).appendTo('body');

this.$form = $('#submitForm');
this.$form.bootstrapValidator()
.on('success.form.bv', function(e) {
e.preventDefault();
++submitted;
});
this.$form.submit(function(e) {
e.preventDefault();
});

submitted = 0;
this.bv = this.$form.data('bootstrapValidator');
this.$username = this.bv.getFieldElements('username');
});

afterEach(function() {
$('#submitForm').bootstrapValidator('destroy').remove();
});

// #481
it('without callback nor remote', function(done) {
$('#sendButton').click();
setTimeout(function() {
expect(submitted).toBe(1);
done();
}, 0);
});

// #481
it('with callback returning true', function(done) {
this.bv.addField('username', {
validators: {
callback: {
message: 'Please enter an username',
callback: function (value, validator, $field) {
return true;
}
}
}
});
$('#sendButton').click();
setTimeout(function() {
expect(submitted).toBe(1);
done();
}, 0);
});

// #481
it('with fake remote returning true', function(done) {
this.bv.addField('username', {
validators: {
fake_remote: {
message: 'Please enter an username',
valid: true
}
}
});
$('#sendButton').click();
setTimeout(function() {
expect(submitted).toBe(1);
done();
}, 100);
});


// #481
it('with callback returning false', function(done) {
this.bv.addField('username', {
validators: {
callback: {
message: 'Please enter an username',
callback: function (value, validator, $field) {
return false;
}
}
}
});
$('#sendButton').click();
setTimeout(function() {
expect(submitted).toBe(0);
done();
}, 0);
});

// #481
it('with fake remote returning false', function(done) {
this.bv.addField('username', {
validators: {
fake_remote: {
message: 'Please enter an username',
valid: false
}
}
});
$('#sendButton').click();
setTimeout(function() {
expect(submitted).toBe(0);
done();
}, 100);
});

});

0 comments on commit f240ea3

Please sign in to comment.