summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/validators
diff options
context:
space:
mode:
authorJiaan <3468028-jiaan@users.noreply.gitlab.com>2019-06-06 07:39:59 +0000
committerKushal Pandya <kushalspandya@gmail.com>2019-06-06 07:39:59 +0000
commitc38ea7e8843660ae9993ad8f515866113bbcc4f5 (patch)
tree243110d63235abff3cb8c0fcc9e4fe9be5dcc51e /app/assets/javascripts/validators
parent5caffe48a9a8bc4d22a9949499cb5040e0f3f673 (diff)
downloadgitlab-ce-c38ea7e8843660ae9993ad8f515866113bbcc4f5.tar.gz
Resolve "Inline validation for user's name and username length"
Diffstat (limited to 'app/assets/javascripts/validators')
-rw-r--r--app/assets/javascripts/validators/input_validator.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/app/assets/javascripts/validators/input_validator.js b/app/assets/javascripts/validators/input_validator.js
new file mode 100644
index 00000000000..f37373977b8
--- /dev/null
+++ b/app/assets/javascripts/validators/input_validator.js
@@ -0,0 +1,34 @@
+const invalidInputClass = 'gl-field-error-outline';
+
+export default class InputValidator {
+ constructor() {
+ this.inputDomElement = {};
+ this.inputErrorMessage = {};
+ this.errorMessage = null;
+ this.invalidInput = null;
+ }
+
+ setValidationStateAndMessage() {
+ this.setValidationMessage();
+
+ const isInvalidInput = !this.inputDomElement.checkValidity();
+ this.inputDomElement.classList.toggle(invalidInputClass, isInvalidInput);
+ this.inputErrorMessage.classList.toggle('hide', !isInvalidInput);
+ }
+
+ setValidationMessage() {
+ if (this.invalidInput) {
+ this.inputDomElement.setCustomValidity(this.errorMessage);
+ this.inputErrorMessage.innerHTML = this.errorMessage;
+ } else {
+ this.resetValidationMessage();
+ }
+ }
+
+ resetValidationMessage() {
+ if (this.inputDomElement.validationMessage === this.errorMessage) {
+ this.inputDomElement.setCustomValidity('');
+ this.inputErrorMessage.innerHTML = this.inputDomElement.title;
+ }
+ }
+}