Created
November 24, 2015 14:10
-
-
Save Langmans/f005b7bbf286445d0fa7 to your computer and use it in GitHub Desktop.
bootstrap-validator extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(['jquery','bootstrap-validator'], function ($) { | |
// add a help block if one doesnt exist yet. | |
$('form:not(.no-validator) .form-group').each(function () { | |
var $group = $(this); | |
if (!$group.find('.help-block.with-errors').length) { | |
if (!$group.find('.help-block').addClass('with-errors').length) { | |
$group.find('div').last().append('<div class="help-block with-errors"></div>'); | |
} | |
// inline forms.. | |
if (!$group.find('.help-block').length) { | |
$group.append('<div class="help-block with-errors"></div>'); | |
} | |
} | |
}); | |
$.fn.validator.Constructor.VALIDATORS.min = function ($el) { | |
var $other = $($el.data('min')); | |
//console.log([$el.val()]); | |
return !$el.val() || $other.length && $el.val() >= $other.val(); | |
}; | |
$.fn.validator.Constructor.VALIDATORS.max = function ($el) { | |
var $other = $($el.data('max')); | |
//console.log([$el.val()]); | |
return !$el.val() || $other.length && $el.val() <= $other.val(); | |
}; | |
// auto required to labels. | |
$('form:not(.no-validator) .form-group label[for]').each(function () { | |
var $label = $(this), | |
$input = $('#' + $label.attr('for')); | |
//console.log($input, $input.is('[required]'), !$label.find('.required').length); | |
if ($input.is('[required]') && !$label.find('.required').length) { | |
$('<strong />', { | |
class: 'required text-danger' | |
}).text('*').appendTo($label); | |
} | |
}); | |
$('form:not(.no-validator)').validator(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment