2016-10-26 04:02:58 -04:00
|
|
|
/* eslint-disable */
|
2016-09-09 08:21:00 -04:00
|
|
|
((global) => {
|
|
|
|
/*
|
|
|
|
* This class overrides the browser's validation error bubbles, displaying custom
|
|
|
|
* error messages for invalid fields instead. To begin validating any form, add the
|
|
|
|
* class `show-gl-field-errors` to the form element, and ensure error messages are
|
|
|
|
* declared in each inputs' title attribute.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* <form class='show-gl-field-errors'>
|
|
|
|
* <input type='text' name='username' title='Username is required.'/>
|
|
|
|
*</form>
|
|
|
|
*
|
|
|
|
* */
|
|
|
|
|
2016-09-23 02:31:04 -04:00
|
|
|
const errorMessageClass = 'gl-field-error';
|
2016-09-09 08:21:00 -04:00
|
|
|
const inputErrorClass = 'gl-field-error-outline';
|
|
|
|
|
2016-09-23 02:31:04 -04:00
|
|
|
class GlFieldError {
|
2016-09-23 04:29:21 -04:00
|
|
|
constructor({ input, formErrors }) {
|
2016-09-23 02:31:04 -04:00
|
|
|
this.inputElement = $(input);
|
|
|
|
this.inputDomElement = this.inputElement.get(0);
|
2016-09-23 04:29:21 -04:00
|
|
|
this.form = formErrors;
|
2016-09-23 02:31:04 -04:00
|
|
|
this.errorMessage = this.inputElement.attr('title') || 'This field is required.';
|
|
|
|
this.fieldErrorElement = $(`<p class='${errorMessageClass} hide'>${ this.errorMessage }</p>`);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
valid: false,
|
|
|
|
empty: true
|
|
|
|
};
|
|
|
|
|
|
|
|
this.initFieldValidation();
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
|
|
|
|
2016-09-23 02:31:04 -04:00
|
|
|
initFieldValidation() {
|
|
|
|
// hidden when injected into DOM
|
2016-09-23 04:29:21 -04:00
|
|
|
this.inputElement.after(this.fieldErrorElement);
|
2016-10-07 05:51:02 -04:00
|
|
|
this.inputElement.off('invalid').on('invalid', this.handleInvalidSubmit.bind(this));
|
2016-10-05 08:28:17 -04:00
|
|
|
this.scopedSiblings = this.safelySelectSiblings();
|
|
|
|
}
|
|
|
|
|
|
|
|
safelySelectSiblings() {
|
|
|
|
// Apply `ignoreSelector` in markup to siblings whose visibility should not be toggled with input validity
|
|
|
|
const ignoreSelector = '.validation-ignore';
|
|
|
|
const unignoredSiblings = this.inputElement.siblings(`p:not(${ignoreSelector})`);
|
|
|
|
const parentContainer = this.inputElement.parent('.form-group');
|
|
|
|
|
|
|
|
// Only select siblings when they're scoped within a form-group with one input
|
|
|
|
const safelyScoped = parentContainer.length && parentContainer.find('input').length === 1;
|
|
|
|
|
|
|
|
return safelyScoped ? unignoredSiblings : this.fieldErrorElement;
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
|
|
|
|
2016-09-23 02:31:04 -04:00
|
|
|
renderValidity() {
|
2016-10-07 05:51:02 -04:00
|
|
|
this.renderClear();
|
2016-09-23 02:31:04 -04:00
|
|
|
|
|
|
|
if (this.state.valid) {
|
2016-10-07 05:51:02 -04:00
|
|
|
return this.renderValid();
|
2016-09-23 02:31:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.state.empty) {
|
2016-10-07 05:51:02 -04:00
|
|
|
return this.renderEmpty();
|
2016-09-23 02:31:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.valid) {
|
2016-10-07 05:51:02 -04:00
|
|
|
return this.renderInvalid();
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
2016-09-23 02:31:04 -04:00
|
|
|
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:51:02 -04:00
|
|
|
handleInvalidSubmit(event) {
|
2016-09-09 08:21:00 -04:00
|
|
|
event.preventDefault();
|
2016-10-07 05:51:02 -04:00
|
|
|
const currentValue = this.accessCurrentValue();
|
2016-09-23 04:29:21 -04:00
|
|
|
this.state.valid = false;
|
2016-09-28 06:59:58 -04:00
|
|
|
this.state.empty = currentValue === '';
|
2016-09-23 02:31:04 -04:00
|
|
|
|
|
|
|
this.renderValidity();
|
2016-09-28 06:59:58 -04:00
|
|
|
this.form.focusOnFirstInvalid.apply(this.form);
|
2016-09-09 08:21:00 -04:00
|
|
|
// For UX, wait til after first invalid submission to check each keyup
|
2016-09-23 02:31:04 -04:00
|
|
|
this.inputElement.off('keyup.field_validator')
|
2016-10-07 15:50:35 -04:00
|
|
|
.on('keyup.field_validator', this.updateValidity.bind(this));
|
2016-09-09 08:21:00 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-07 05:51:02 -04:00
|
|
|
/* Get or set current input value */
|
|
|
|
accessCurrentValue(newVal) {
|
|
|
|
return newVal ? this.inputElement.val(newVal) : this.inputElement.val();
|
|
|
|
}
|
|
|
|
|
2016-09-23 02:31:04 -04:00
|
|
|
getInputValidity() {
|
|
|
|
return this.inputDomElement.validity.valid;
|
|
|
|
}
|
2016-09-09 08:21:00 -04:00
|
|
|
|
2016-10-07 05:51:02 -04:00
|
|
|
updateValidity() {
|
|
|
|
const inputVal = this.accessCurrentValue();
|
2016-09-28 06:59:58 -04:00
|
|
|
this.state.empty = !inputVal.length;
|
2016-09-23 04:29:21 -04:00
|
|
|
this.state.valid = this.getInputValidity();
|
2016-09-23 02:31:04 -04:00
|
|
|
this.renderValidity();
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:51:02 -04:00
|
|
|
renderValid() {
|
|
|
|
return this.renderClear();
|
2016-09-23 02:31:04 -04:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:51:02 -04:00
|
|
|
renderEmpty() {
|
|
|
|
return this.renderInvalid();
|
2016-09-23 02:31:04 -04:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:51:02 -04:00
|
|
|
renderInvalid() {
|
2016-09-23 04:29:21 -04:00
|
|
|
this.inputElement.addClass(inputErrorClass);
|
2016-10-05 08:28:17 -04:00
|
|
|
this.scopedSiblings.hide();
|
2016-09-23 04:29:21 -04:00
|
|
|
return this.fieldErrorElement.show();
|
2016-09-23 02:31:04 -04:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:51:02 -04:00
|
|
|
renderClear() {
|
|
|
|
const inputVal = this.accessCurrentValue();
|
2016-09-23 04:29:21 -04:00
|
|
|
if (!inputVal.split(' ').length) {
|
2016-10-05 08:28:52 -04:00
|
|
|
const trimmedInput = inputVal.trim();
|
2016-10-07 05:51:02 -04:00
|
|
|
this.accessCurrentValue(trimmedInput);
|
2016-09-23 04:29:21 -04:00
|
|
|
}
|
|
|
|
this.inputElement.removeClass(inputErrorClass);
|
2016-10-05 08:28:17 -04:00
|
|
|
this.scopedSiblings.hide();
|
2016-09-23 04:29:21 -04:00
|
|
|
this.fieldErrorElement.hide();
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
2016-09-23 02:31:04 -04:00
|
|
|
}
|
2016-09-09 08:21:00 -04:00
|
|
|
|
2016-09-23 04:29:21 -04:00
|
|
|
const customValidationFlag = 'no-gl-field-errors';
|
|
|
|
|
2016-09-23 02:31:04 -04:00
|
|
|
class GlFieldErrors {
|
|
|
|
constructor(form) {
|
|
|
|
this.form = $(form);
|
2016-09-23 04:29:21 -04:00
|
|
|
this.state = {
|
|
|
|
inputs: [],
|
|
|
|
valid: false
|
|
|
|
};
|
2016-09-23 02:31:04 -04:00
|
|
|
this.initValidators();
|
|
|
|
}
|
2016-09-09 08:21:00 -04:00
|
|
|
|
2016-09-23 02:31:04 -04:00
|
|
|
initValidators () {
|
2016-10-17 05:44:25 -04:00
|
|
|
// register selectors here as needed
|
|
|
|
const validateSelectors = [':text', ':password', '[type=email]']
|
|
|
|
.map((selector) => `input${selector}`).join(',');
|
|
|
|
|
|
|
|
this.state.inputs = this.form.find(validateSelectors).toArray()
|
2016-09-23 04:29:21 -04:00
|
|
|
.filter((input) => !input.classList.contains(customValidationFlag))
|
|
|
|
.map((input) => new GlFieldError({ input, formErrors: this }));
|
2016-09-23 02:31:04 -04:00
|
|
|
|
|
|
|
this.form.on('submit', this.catchInvalidFormSubmit);
|
|
|
|
}
|
2016-09-09 08:21:00 -04:00
|
|
|
|
2016-09-23 02:31:04 -04:00
|
|
|
/* Neccessary to prevent intercept and override invalid form submit
|
|
|
|
* because Safari & iOS quietly allow form submission when form is invalid
|
|
|
|
* and prevents disabling of invalid submit button by application.js */
|
|
|
|
|
|
|
|
catchInvalidFormSubmit (event) {
|
|
|
|
if (!event.currentTarget.checkValidity()) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
focusOnFirstInvalid () {
|
2016-09-28 06:59:58 -04:00
|
|
|
const firstInvalid = this.state.inputs.filter((input) => !input.inputDomElement.validity.valid)[0];
|
|
|
|
firstInvalid.inputElement.focus();
|
2016-09-09 08:21:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
global.GlFieldErrors = GlFieldErrors;
|
|
|
|
|
|
|
|
})(window.gl || (window.gl = {}));
|