webpack-bootstrap-ui-kit/src/js/_components/_ui.form.validate.field.js

143 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-06-08 17:20:51 +02:00
import $ from 'jquery';
2020-05-13 18:05:34 +02:00
import Events from '../_events';
import FormValidateField from './_ui.form.validate.field';
import SpinnerUI from './_ui.spinner';
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
const FormValidate = (($) => {
2019-06-08 17:20:51 +02:00
// Constants
2020-05-13 18:05:34 +02:00
const NAME = 'jsFormValidate';
2019-06-08 17:20:51 +02:00
const DATA_KEY = NAME;
const $Html = $('html, body');
2020-05-13 18:05:34 +02:00
class FormValidate {
constructor(element) {
2019-06-08 17:20:51 +02:00
const ui = this;
2020-05-13 18:05:34 +02:00
const $element = $(element);
const $fields = $element.find('input,textarea,select');
2020-05-13 18:05:34 +02:00
ui._element = element;
$element.data(DATA_KEY, this);
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
ui._fields = $fields;
ui._stepped_form = $element.data('jsSteppedForm');
2019-06-08 17:20:51 +02:00
// prevent browsers checks (will do it using JS)
2020-05-13 18:05:34 +02:00
$element.attr('novalidate', 'novalidate');
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
$element.on(Events.FORM_INIT_STEPPED, () => {
ui._stepped_form = $element.data('jsSteppedForm');
2019-06-08 17:20:51 +02:00
});
2020-05-13 18:05:34 +02:00
// init fields validation
$fields.each((i, el) => {
// skip some fields here
if ($(el).attr('role') === 'combobox') {
return;
}
2020-05-13 18:05:34 +02:00
new FormValidateField(el);
});
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
$element.removeClass('error');
// check form
$element.on('submit', (e) => {
ui.validate(true, () => {
e.preventDefault();
2019-07-10 20:59:57 +02:00
2020-05-13 18:05:34 +02:00
// switch to step
if (ui._stepped_form) {
const $el = $element.find('.error').first();
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
if ($el.length) {
ui._stepped_form.step($el.parents('.step'));
}
}
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
$element.addClass('error');
SpinnerUI.hide();
2019-07-10 20:59:57 +02:00
2020-05-13 18:05:34 +02:00
$element.trigger(Events.FORM_VALIDATION_FAILED);
2019-06-08 17:20:51 +02:00
});
2020-05-13 18:05:34 +02:00
});
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
$element.addClass(`${NAME}-active`);
$element.trigger(Events.FORM_INIT_VALIDATE);
2019-06-08 17:20:51 +02:00
}
2020-05-13 18:05:34 +02:00
// Public methods
dispose() {
const $element = $(this._element);
2019-07-10 20:59:57 +02:00
2020-05-13 18:05:34 +02:00
$element.removeClass(`${NAME}-active`);
$.removeData(this._element, DATA_KEY);
this._element = null;
2019-06-08 17:20:51 +02:00
}
2020-05-13 18:05:34 +02:00
validate(scrollTo = true, badCallback = false) {
console.log('Checking the field ...');
2019-06-08 17:20:51 +02:00
const ui = this;
2020-05-13 18:05:34 +02:00
let valid = true;
2020-05-13 18:05:34 +02:00
ui._fields.each((i, el) => {
const $el = $(el);
const fieldUI = $el.data('jsFormValidateField');
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
if (fieldUI && !fieldUI.validate()) {
SpinnerUI.hide();
2020-05-13 18:05:34 +02:00
console.log('Invalid field data:');
console.log($el);
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
if (badCallback) {
badCallback();
}
2020-05-13 18:05:34 +02:00
valid = false;
return false;
}
});
2019-06-08 17:20:51 +02:00
2020-05-13 18:05:34 +02:00
return valid;
2019-06-08 17:20:51 +02:00
}
static _jQueryInterface() {
return this.each(function() {
2020-05-13 18:05:34 +02:00
// attach functionality to element
const $element = $(this);
let data = $element.data(DATA_KEY);
2019-06-08 17:20:51 +02:00
if (!data) {
2020-05-13 18:05:34 +02:00
data = new FormValidate(this);
$element.data(DATA_KEY, data);
2019-06-08 17:20:51 +02:00
}
});
}
}
// jQuery interface
2020-05-13 18:05:34 +02:00
$.fn[NAME] = FormValidate._jQueryInterface;
$.fn[NAME].Constructor = FormValidate;
2019-06-08 17:20:51 +02:00
$.fn[NAME].noConflict = function() {
$.fn[NAME] = JQUERY_NO_CONFLICT;
2020-05-13 18:05:34 +02:00
return FormValidate._jQueryInterface;
2019-06-08 17:20:51 +02:00
};
2020-05-13 18:05:34 +02:00
// auto-apply
$(window).on(`${Events.AJAX} ${Events.LOADED}`, () => {
$('form').each((i, el) => {
const $el = $(el);
// skip some forms
if ($el.hasClass('no-validation')) {
return true;
}
$el.jsFormValidate();
});
});
return FormValidate;
2019-06-08 17:20:51 +02:00
})($);
2020-05-13 18:05:34 +02:00
export default FormValidate;