From b74a28b2d840d45477efa35dc7ffb6d518cb6964 Mon Sep 17 00:00:00 2001 From: adilshow Date: Fri, 13 Jan 2017 16:10:52 +0000 Subject: [PATCH 1/3] fixing options persistence at new calls the problem was that options were not been saved, because at merge of options (default, current, new) the current options were not considered as were lost on the process, "$this.data()" didn't work at least for this purpose. the second problem was that the constructor of new validator was depend of "if(!data)" that was only true on the first call, what caused to call with new options were never saved at data. --- js/validator.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/js/validator.js b/js/validator.js index 78b0bde..9c74a50 100644 --- a/js/validator.js +++ b/js/validator.js @@ -377,11 +377,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]() }) } From 092c37e119eeea42935058bf3cd733b94160e61f Mon Sep 17 00:00:00 2001 From: adilshow Date: Fri, 13 Jan 2017 17:24:28 +0000 Subject: [PATCH 2/3] remove dependency on required remove dependency on required so multiple input custom validations don't have double message example: Input telephone and mobilephone either one of the is required but not both. In the case of fill only one filed, the other would get required validator error. --- js/validator.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/js/validator.js b/js/validator.js index 9c74a50..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) From 58af2ff84f9773d97917058e64d2588dd1997d0f Mon Sep 17 00:00:00 2001 From: adilshow Date: Mon, 16 Jan 2017 17:09:09 +0000 Subject: [PATCH 3/3] add possible bug test --- js/tests/unit/validator.js | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) 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') + }) })