Last active
August 29, 2015 14:13
-
-
Save Langmans/817f0313ab421cbe7ee7 to your computer and use it in GitHub Desktop.
bootstrap.validator.invisible-elements.js
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
//autocompress:no | |
/*! | |
* Disables invisible (non focusable elements) form fields inside forms while validating. | |
* | |
* @author Ruben Vincenten <[email protected]> | |
* @see https://github.com/1000hz/bootstrap-validator | |
*/ | |
jQuery(function ($) { | |
var | |
dataKey = 'validate.bs.skip', | |
elementSelector = ':input:not([type=hidden]):hidden, :input([novalidate])'; | |
$('body').on({ | |
// Disable browser error about input not being focusable. | |
'submit': function () { | |
$(this).find(elementSelector).attr({ | |
disabled: true, | |
required: false | |
}); | |
}, | |
// Before validating each input field, | |
// save the state of all hidden inputs and | |
// disable them plus make them not required. | |
'validate.bs.validator': function () { | |
var skips = []; | |
$(this) | |
.find(elementSelector).each(function () { | |
skips.push({ | |
element: this, | |
required: this.required, | |
disabled: this.disabled | |
}); | |
this.disabled = true; | |
this.required = false; | |
}).end() | |
.data(dataKey, skips); | |
}, | |
// At this point the visibility of the submit button is set, | |
// so restore state of invisible elements. | |
'validated.bs.validator': function () { | |
var | |
$this = $(this), | |
skips = $this.data(dataKey), | |
skip, element; | |
while (skip = skips.shift()) { | |
element = skip.element; | |
element.required = skip.required; | |
element.disabled = skip.disabled; | |
} | |
console.log('------------'); | |
$this.removeData(dataKey); | |
} | |
}, | |
'form[data-toggle="validator"]' | |
); | |
}) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment