Last active
April 17, 2020 16:26
-
-
Save justin-lyon/6595707c06653ef4c428450e210ee3c3 to your computer and use it in GitHub Desktop.
lightning input validation
This file contains hidden or 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
({ | |
validateForm: function(cmp) { | |
// Get All lightning:input elements with aura:id "input" | |
var inputs = cmp.find("input"); | |
// Iterate over array of input components | |
// `acc` is a local variable, defined as a boolean in this case | |
// `input` is the input component during iteration | |
var isValid = inputs.reduce(function(acc, input) { | |
input.showHelpMessageIfInvalid(); | |
return acc && input.get("v.validity").valid; | |
}, true); // acc starts as true | |
return isValid; | |
} | |
}) |
This file contains hidden or 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
validate () { | |
const requiredInputs = this.template.querySelectorAll('.required') | |
const isValid = [ ...requiredInputs ].reduce((validity, el) => { | |
const report = el.reportValidity() | |
return validity && report | |
}, true) | |
return isValid | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment