Created
August 10, 2017 13:58
-
-
Save fvdb/cc93da2e99312fa67844f5fa9bc1529d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks if the textarea is valid | |
*/ | |
isValid() { | |
const { node, maximum, textarea } = this; | |
const { length: inputLength } = textarea.value; | |
if (!node.hasAttribute('data-required')) return true; | |
try { | |
guardRequired(node, inputLength); | |
guardMaxLength(node, inputLength, maximum); | |
} catch (errorMessage) { | |
this.errorMessage = errorMessage; | |
return false; | |
} | |
return true; | |
} | |
guardRequired(node, inputLength) { | |
if (inputLength === 0) { | |
throw node.getAttribute('data-message_required'); | |
} | |
} | |
guardMaxLength(node, inputLength, maximum) { | |
if (maximum && inputLength > maximum) { | |
throw node.getAttribute('data-message_max'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment