I am creating an input form with VueJS and the form contains about 10+ input fields. I have a reset button that, when clicked, will "reset" the form's value to either an empty string, null, or boolean (i have checkboxes in actual form).
What I have currently works fine, but I can't help and wonder that this method is going to be hard to maintain once I get additional fields added to the form. Every time I get a new field to add, I will have to also update my reset method with that new field. *I do want to keep things simple, so I also don't want to over complicate things, either...so maybe keeping this approach is easier to read for junior devs..
Is this a good approach?
Here's my current code:
Script section:
..snip...
data() {
return {
show: true,
products: [],
subjects: [],
form: {
product: '',
subject: ''
}
}
},
created() {
this.getProducts() <--- getting products from an api
this.getSubjects() < -- getting subjects from an api
..snip..
},
methods: {
resetForm() {
this.form = {
product: '',
subject: ''
}
// Trick to reset/clear native browser form validation state
this.show = false
this.$nextTick(() => {
this.show = true
})
}
...snip...
Template section:
<form v-if="show">
..snip..
<label for="product_select_input">Product:</label>
<select
id="product_select_input"
v-model="form.product"
required
>
<option disabled value="">Select</option>
<option
v-for="product in products"
:key="product.product_id"
:value="product.product_id"
>{{ product.product_name }}</option
>
</select>
..snip..
<button @click.prevent="resetForm">
Reset Form
</button>
...snip...
</form>