5👍
✅
I can see multiple issues in your code:
1) Don’t apply jQuery-like approach for getting input values. Use v-model
instead. This will simplify your code
<template>
<input v-model="form.firstName" type="text"/>
</template>
<script>
export default {
data: {
form: {
firstName: '',
}
},
methods: {
validateBeforeSubmit() {
// take `firstName` directly from `data` not need for `getElementById`
const firstName = this.form.firstName;
}
},
}
</script>
2) Remove validateBeforeSubmit
and saveForm
from ready
. Ready hook is obsolete in vue@2
. And also it makes no sense. It’s better to call it on form @submit
.
3) It’s better to create array using []
syntax instead of new Array()
4) Always provide name for your component for easier debug
export default {
name: 'ValidationForm',
}
5) I don’t know where was an issue but it works. Check this link below. I have updated your code. Try to submit form and check the console:
https://codesandbox.io/s/w6jl619qr5?expanddevtools=1&module=%2Fsrc%2Fcomponents%2FForm.vue
Source:stackexchange.com