new project Vue component is responsible for form validation

This commit is contained in:
dandds
2018-09-24 14:27:19 -04:00
parent 293f94cae1
commit 729c96de3a
2 changed files with 34 additions and 1 deletions

View File

@@ -32,6 +32,7 @@ export default {
).map(createEnvironment)
return {
showError: false,
environments,
name,
}
@@ -41,6 +42,12 @@ export default {
this.$root.$on('onEnvironmentAdded', this.addEnvironment)
},
updated: function() {
if (this.environmentsHaveNames()) {
this.showError = false
}
},
methods: {
addEnvironment: function (event) {
this.environments.push(createEnvironment(""))
@@ -50,6 +57,29 @@ export default {
if (this.environments.length > 1) {
this.environments.splice(index, 1)
}
},
environmentsHaveNames: function () {
return this.environments.every((e) => e.name !== "")
},
validateAndOpenModal: function (modalName) {
const textInputs = this.$children.reduce((previous, newVal) => {
// display textInput error if it is not valid
if (!newVal.showValid) {
newVal.showError = true
}
return newVal.showValid && previous
}, true)
const isValid = textInputs && this.environmentsHaveNames()
if (isValid) {
this.openModal(modalName)
} else {
this.showError = true
}
}
}
}