Skip to content

Instantly share code, notes, and snippets.

@Langmans
Last active August 29, 2015 14:13
Show Gist options
  • Save Langmans/817f0313ab421cbe7ee7 to your computer and use it in GitHub Desktop.
Save Langmans/817f0313ab421cbe7ee7 to your computer and use it in GitHub Desktop.
bootstrap.validator.invisible-elements.js
//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