2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-10-10 04:09:01 -04:00
|
|
|
import GlFieldError from './gl_field_error';
|
2016-11-01 13:40:48 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
const customValidationFlag = 'gl-field-error-ignore';
|
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
export default class GlFieldErrors {
|
2017-03-11 02:30:44 -05:00
|
|
|
constructor(form) {
|
|
|
|
this.form = $(form);
|
|
|
|
this.state = {
|
|
|
|
inputs: [],
|
2017-10-10 04:09:01 -04:00
|
|
|
valid: false,
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
this.initValidators();
|
|
|
|
}
|
2016-09-09 08:21:00 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
initValidators() {
|
2017-03-11 02:30:44 -05:00
|
|
|
// register selectors here as needed
|
2019-04-01 09:06:58 -04:00
|
|
|
const validateSelectors = [':text', ':password', '[type=email]', '[type=url]', '[type=number]']
|
2020-12-23 19:10:25 -05:00
|
|
|
.map((selector) => `input${selector}`)
|
2018-10-24 15:17:03 -04:00
|
|
|
.join(',');
|
2016-10-17 05:44:25 -04:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
this.state.inputs = this.form
|
|
|
|
.find(validateSelectors)
|
|
|
|
.toArray()
|
2020-12-23 19:10:25 -05:00
|
|
|
.filter((input) => !input.classList.contains(customValidationFlag))
|
|
|
|
.map((input) => new GlFieldError({ input, formErrors: this }));
|
2016-09-23 02:31:04 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
this.form.on('submit', GlFieldErrors.catchInvalidFormSubmit);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-09-09 08:21:00 -04:00
|
|
|
|
2018-10-30 06:53:01 -04:00
|
|
|
/* Necessary to prevent intercept and override invalid form submit
|
2017-03-11 02:30:44 -05:00
|
|
|
* because Safari & iOS quietly allow form submission when form is invalid
|
|
|
|
* and prevents disabling of invalid submit button by application.js */
|
2016-09-23 02:31:04 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
static catchInvalidFormSubmit(e) {
|
|
|
|
const $form = $(e.currentTarget);
|
2017-05-25 05:35:51 -04:00
|
|
|
|
|
|
|
if (!$form.attr('novalidate')) {
|
2017-10-10 05:47:29 -04:00
|
|
|
if (!e.currentTarget.checkValidity()) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2017-05-25 05:35:51 -04:00
|
|
|
}
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-09-09 08:21:00 -04:00
|
|
|
|
2017-05-07 18:35:56 -04:00
|
|
|
/* Public method for triggering validity updates manually */
|
|
|
|
updateFormValidityState() {
|
2020-12-23 19:10:25 -05:00
|
|
|
this.state.inputs.forEach((field) => {
|
2017-05-07 18:35:56 -04:00
|
|
|
if (field.state.submitted) {
|
|
|
|
field.updateValidity();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-06 05:09:20 -04:00
|
|
|
get invalidInputs() {
|
|
|
|
return this.state.inputs.filter(
|
|
|
|
({
|
|
|
|
inputDomElement: {
|
|
|
|
validity: { valid },
|
|
|
|
},
|
|
|
|
}) => !valid,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get focusedInvalidInput() {
|
|
|
|
return this.invalidInputs.find(({ inputElement }) => inputElement.is(':focus'));
|
|
|
|
}
|
|
|
|
|
|
|
|
focusInvalid() {
|
|
|
|
if (this.focusedInvalidInput) return;
|
|
|
|
|
|
|
|
this.invalidInputs[0].inputElement.focus();
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|