diff options
author | Jiaan <3468028-jiaan@users.noreply.gitlab.com> | 2019-06-06 07:39:59 +0000 |
---|---|---|
committer | Kushal Pandya <kushalspandya@gmail.com> | 2019-06-06 07:39:59 +0000 |
commit | c38ea7e8843660ae9993ad8f515866113bbcc4f5 (patch) | |
tree | 243110d63235abff3cb8c0fcc9e4fe9be5dcc51e /app/assets/javascripts/validators | |
parent | 5caffe48a9a8bc4d22a9949499cb5040e0f3f673 (diff) | |
download | gitlab-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.js | 34 |
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; + } + } +} |