diff --git a/js/tests/unit/validator.js b/js/tests/unit/validator.js index 836525f..3d58622 100644 --- a/js/tests/unit/validator.js +++ b/js/tests/unit/validator.js @@ -736,4 +736,45 @@ $(function () { assert.ok($form.find('.form-group').hasClass('has-error'), '.has-error class is not removed from form-group') }) + + QUnit.test('call custom validator', function (assert) { + + var form = '
' + + '
' + + '' + + '' + + '
Fill at least one
' + + '
' + + '
' + + var $form = $(form) + .appendTo('#qunit-fixture') + .validator() + + var options1 = { + custom: { + validation1: function (element) { } + } + } + + var options2 = { + custom: { + validation2: function (element) { } + } + }; + + var form = $('form').validator(options1) + + var validator = $(form).data("bs.validator") + + assert.ok(validator.options.custom.validation1 && validator.options.custom.validation1.toString() == options1.custom.validation1.toString(), 'validator.options.custom contains validation1') + + $('form').validator('destroy') + + $('form').validator(options2) + + validator = $("form").data("bs.validator") + + assert.ok(validator.options.custom.validation2 && validator.options.custom.validation2.toString() == options2.custom.validation2.toString(), 'validator.options.custom contains validation2') + }) }) diff --git a/js/validator.js b/js/validator.js index 78b0bde..bc0cfcf 100644 --- a/js/validator.js +++ b/js/validator.js @@ -203,8 +203,9 @@ $.each(this.validators, $.proxy(function (key, validator) { var error = null - if ((getValue($el) || $el.attr('required')) && - ($el.attr('data-' + key) !== undefined || key == 'native') && + if (((getValue($el) || $el.attr('required')) && + ($el.attr('data-' + key) !== undefined || key == 'native') || + $el.data(key) && key != 'native' && key != 'required') && (error = validator.call(this, $el))) { error = getErrorMessage(key) || error !~errors.indexOf(error) && errors.push(error) @@ -377,11 +378,14 @@ function Plugin(option) { return this.each(function () { var $this = $(this) - var options = $.extend({}, Validator.DEFAULTS, $this.data(), typeof option == 'object' && option) + var dataOptions = $this.data() && $this.data()["bs.validator"] && $this.data()["bs.validator"].options + ? $this.data()["bs.validator"].options : {}; + + var options = $.extend({}, Validator.DEFAULTS, $this.data(), dataOptions, typeof option == 'object' && option) var data = $this.data('bs.validator') if (!data && option == 'destroy') return - if (!data) $this.data('bs.validator', (data = new Validator(this, options))) + $this.data('bs.validator', (data = new Validator(this, options))) if (typeof option == 'string') data[option]() }) }