0👍
✅
The click
-handler updates currentStage
without first validating the form. However, the validation occurs in appendFields
, which is computed after buttonClicked()
. The validation should be the first step, which can block the proceeding steps.
I would refactor it like this:
-
Make
appendFields
a componentmethod
, since it’s not really a computed property (especially because it returns nothing). -
Move the
currentStage
update into its own function for clarity. -
Move the form validation from
appendFields()
to the button’sclick
-handler. -
In the
click
-handler, call the functions created in step 1 and 2 if the form is valid.
export default {
methods: {
// 1️⃣
appendFields() {
if (this.jsonStatham.hasOwnProperty(this.traffic.unique1)) {
this.integrationParams.push(...Object.keys(this.jsonStatham[this.traffic.unique1]))
}
},
// 2️⃣
updateStage(value) {
if (value === 0) {
this.currentStage++
} else if (value === 1) {
this.currentStage--
}
},
buttonClicked(value) {
// 3️⃣
this.v$.$validate()
if (!this.v$.$error) {
// 4️⃣
this.appendFields()
this.updateStage(value)
} else {
alert("Error, Not all fields are filled in.")
}
}
}
}
Also be aware that useValidate()
is intended for the Composition API, so it should be called inside setup()
:
export default {
setup() {
return {
v$: useValidate()
}
}
}
Source:stackexchange.com