**how can I check if the input fields are completed when clicking the Submit button? I'd like to alert if one of the inputs isn't completed or, if they all are completed - display a component in the App.vue file. But my validation checking seems not to be working.
<template>
<button class="btn" @click="submit">Submit</button>
</template>
<script>
export default {
methods: {
submit() {
if (
!this.firstName &&
!this.lastName &&
!this.paymentAmount &&
!this.accountNumber
) {
this.$emit("final");
} else {
alert("please fill the required fields");
}
},
},
inject: ["firstName", "lastName", "paymentAmount", "accountNumber"],
};
</script>
<style scoped>
.btn {
padding: 10px 30px;
}
</style>
https://codesandbox.io/s/intelligent-jasper-teh1im?file=/src/components/Button.vue**
You just need to wrap your inputs inside a <form>
element (lowercased!) to get native form validation.
// Form.vue
<template>
<form ref="myForm">
<KeepAlive>
<component :is="currentStep" @previous="previousStep" @next="nextStep" />
</KeepAlive>
</form>
</template>
You can also check the validity programatically by using this.$refs.myForm. checkValidity()
.