I have a form with submit prevent enabled:
<form method="get" :action="urls.baseUrl" ref="productsFilterForm" @submit.prevent>
Form submit button looks like this:
<button type="submit" @click="submitForm" class="btn">{{ translations['search'] }}</button>
SubmitForm function:
submitForm() {
this.validateFilterValues();
let form = this.$refs.productsFilterForm as HTMLFormElement;
form.submit();
}
validateFilterValues:
validateFilterValues() {
for (const [key, filter] of Object.entries(this.filters))
filter.enabled = !(filter.value === null || filter.value === '' || filter.value == 0);
}
}
filters data looks like this:
private filters: Record<string, Input> = {
categories: { enabled: true, value: null },
productId: { enabled: true, value: null },
modificationId: { enabled: true, value: null },
title: { enabled: true, value: null },
barcode: { enabled: true, value: null },
status: { enabled: true, value: null },
descriptionStatus: { enabled: true, value: null },
supplierCode: { enabled: true, value: null }
}
Filter data goes into input component:
<input-component id="formProductId"
type="number"
name="productId"
:label="translations['product_id']"
:placeholder="translations['product_id']"
:enabled="filters.productId.enabled"
:value="filters.productId.value"
/>
And input component looks like this:
<div class="form-element">
<label :for="id" :class="['form-element-title', { 'js-focused': focused }]">{{ label }}</label>
<input :id="id" :name="enabled ? name : false" v-model="inputData" type="text" :placeholder="placeholder" @focus="focused = true" @blur="focused = false">
</div>
Form submit works but there is a problem. validateFilterValues function removes the name of the inputs if they are empty. If I console.log the form variable before submitting it. I see that the empty inputs have their names removed. But the problem is that those inputs still submit through the form into the backend side. What am I missing here?
Also if I use the validation method on submit - it works fine:
<form method="get" :action="urls.baseUrl" @submit="validateFilterValues()">
But I need to make the submit as a separate function and prevent enter key form submit.