2017-03-11 02:30:44 -05:00
|
|
|
/**
|
|
|
|
* 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 `gl-show-field-errors` to the form element, and ensure error messages are
|
|
|
|
* declared in each inputs' `title` attribute. If no title is declared for an invalid
|
|
|
|
* field the user attempts to submit, "This field is required." will be shown by default.
|
|
|
|
*
|
|
|
|
* Opt not to validate certain fields by adding the class `gl-field-error-ignore` to the input.
|
|
|
|
*
|
|
|
|
* Set a custom error anchor for error message to be injected after with the
|
|
|
|
* class `gl-field-error-anchor`
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
|
|
|
* Basic:
|
|
|
|
*
|
|
|
|
* <form class='gl-show-field-errors'>
|
|
|
|
* <input type='text' name='username' title='Username is required.'/>
|
|
|
|
* </form>
|
|
|
|
*
|
|
|
|
* Ignore specific inputs (e.g. UsernameValidator):
|
|
|
|
*
|
|
|
|
* <form class='gl-show-field-errors'>
|
|
|
|
* <div class="form-group>
|
|
|
|
* <input type='text' class='gl-field-errors-ignore' pattern='[a-zA-Z0-9-_]+'/>
|
|
|
|
* </div>
|
|
|
|
* <div class="form-group">
|
|
|
|
* <input type='text' name='username' title='Username is required.'/>
|
|
|
|
* </div>
|
|
|
|
* </form>
|
|
|
|
*
|
|
|
|
* Custom Error Anchor (allows error message to be injected after specified element):
|
|
|
|
*
|
|
|
|
* <form class='gl-show-field-errors'>
|
|
|
|
* <div class="form-group gl-field-error-anchor">
|
|
|
|
* <input type='text' name='username' title='Username is required.'/>
|
|
|
|
* // Error message typically injected here
|
|
|
|
* </div>
|
|
|
|
* // Error message now injected here
|
|
|
|
* </form>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regex Patterns in use:
|
|
|
|
*
|
|
|
|
* Only alphanumeric: : "[a-zA-Z0-9]+"
|
|
|
|
* No special characters : "[a-zA-Z0-9-_]+",
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
const errorMessageClass = 'gl-field-error';
|
|
|
|
const inputErrorClass = 'gl-field-error-outline';
|
|
|
|
const errorAnchorSelector = '.gl-field-error-anchor';
|
|
|
|
const ignoreInputSelector = '.gl-field-error-ignore';
|
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
export default class GlFieldError {
|
2017-03-11 02:30:44 -05:00
|
|
|
constructor({ input, formErrors }) {
|
|
|
|
this.inputElement = $(input);
|
|
|
|
this.inputDomElement = this.inputElement.get(0);
|
|
|
|
this.form = formErrors;
|
|
|
|
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,
|
2017-05-07 18:35:56 -04:00
|
|
|
submitted: false,
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
this.initFieldValidation();
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
initFieldValidation() {
|
|
|
|
const customErrorAnchor = this.inputElement.parents(errorAnchorSelector);
|
|
|
|
const errorAnchor = customErrorAnchor.length ? customErrorAnchor : this.inputElement;
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
// hidden when injected into DOM
|
|
|
|
errorAnchor.after(this.fieldErrorElement);
|
|
|
|
this.inputElement.off('invalid').on('invalid', this.handleInvalidSubmit.bind(this));
|
|
|
|
this.scopedSiblings = this.safelySelectSiblings();
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
safelySelectSiblings() {
|
|
|
|
// Apply `ignoreSelector` in markup to siblings whose visibility should not be toggled
|
|
|
|
const unignoredSiblings = this.inputElement.siblings(`p:not(${ignoreInputSelector})`);
|
|
|
|
const parentContainer = this.inputElement.parent('.form-group');
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
// Only select siblings when they're scoped within a form-group with one input
|
|
|
|
const safelyScoped = parentContainer.length && parentContainer.find('input').length === 1;
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
return safelyScoped ? unignoredSiblings : this.fieldErrorElement;
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
renderValidity() {
|
|
|
|
this.renderClear();
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
if (this.state.valid) {
|
|
|
|
this.renderValid();
|
|
|
|
} else if (this.state.empty) {
|
|
|
|
this.renderEmpty();
|
|
|
|
} else if (!this.state.valid) {
|
|
|
|
this.renderInvalid();
|
2016-10-27 09:49:07 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
handleInvalidSubmit(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const currentValue = this.accessCurrentValue();
|
|
|
|
this.state.valid = false;
|
|
|
|
this.state.empty = currentValue === '';
|
2017-05-07 18:35:56 -04:00
|
|
|
this.state.submitted = true;
|
2017-03-11 02:30:44 -05:00
|
|
|
this.renderValidity();
|
|
|
|
this.form.focusOnFirstInvalid.apply(this.form);
|
2017-05-07 18:35:56 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
// For UX, wait til after first invalid submission to check each keyup
|
|
|
|
this.inputElement.off('keyup.fieldValidator')
|
|
|
|
.on('keyup.fieldValidator', this.updateValidity.bind(this));
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
/* Get or set current input value */
|
|
|
|
accessCurrentValue(newVal) {
|
|
|
|
return newVal ? this.inputElement.val(newVal) : this.inputElement.val();
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
getInputValidity() {
|
|
|
|
return this.inputDomElement.validity.valid;
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
updateValidity() {
|
|
|
|
const inputVal = this.accessCurrentValue();
|
|
|
|
this.state.empty = !inputVal.length;
|
|
|
|
this.state.valid = this.getInputValidity();
|
|
|
|
this.renderValidity();
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
renderValid() {
|
|
|
|
return this.renderClear();
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
renderEmpty() {
|
|
|
|
return this.renderInvalid();
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
renderInvalid() {
|
|
|
|
this.inputElement.addClass(inputErrorClass);
|
|
|
|
this.scopedSiblings.hide();
|
|
|
|
return this.fieldErrorElement.show();
|
|
|
|
}
|
2016-10-27 09:49:07 -04:00
|
|
|
|
2017-03-11 02:30:44 -05:00
|
|
|
renderClear() {
|
|
|
|
const inputVal = this.accessCurrentValue();
|
|
|
|
if (!inputVal.split(' ').length) {
|
|
|
|
const trimmedInput = inputVal.trim();
|
|
|
|
this.accessCurrentValue(trimmedInput);
|
2016-10-27 09:49:07 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
this.inputElement.removeClass(inputErrorClass);
|
|
|
|
this.scopedSiblings.hide();
|
|
|
|
this.fieldErrorElement.hide();
|
2016-10-27 09:49:07 -04:00
|
|
|
}
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|