Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

remove dependency on required, possibility to add new options without the need to distroy validator, added tests #608

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions js/tests/unit/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<form>'
+ '<div class="form-group has-error">'
+ '<input type="text" value="example" data-validation1 data-validation2>'
+ '<input type="text" value="mobilephone" data-requiredone="phone">'
+ '<div class="help-block with-errors">Fill at least one</div>'
+ '</div>'
+ '</form>'

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')
})
})
12 changes: 8 additions & 4 deletions js/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]()
})
}
Expand Down