I am currently studying front-end javascript development and current doing a personal project using VueJS, Vuetify and Vuelidate.
I have a simple question.
I have two text fields as below.
<v-layout wrap>
<v-flex>
<v-text-field
label="First Name"
maxlength="20"
v-model.trim="firstName"
:error-messages="firstNameErrors"
@input="$v.firstName.$touch()"
@blur="$v.firstName.$touch()"
></v-text-field>
</v-flex>
<v-flex>
<v-text-field
label="Last Name"
v-model.trim="lastName"
maxlength="20"
:error-messages="lastNameErrors"
@input="$v.lastName.$touch()"
@blur="$v.lastName.$touch()"
></v-text-field>
</v-flex>
</v-layout>
I have a vuelidate written in the computed hook as below.
computed: {
firstNameErrors () {
const errors = []
if (!this.$v.firstName.$dirty) return errors
!this.$v.firstName.required && errors.push('First Name is required')
return errors
},
lastNameErrors () {
const errors = []
if (!this.$v.lastName.$dirty) return errors
!this.$v.lastName.required && errors.push('Last Name is required')
return errors
}
The code works fine and does what is supposed to do, generate validation errors, "required" in my case.
My question is, I would like to include both validation error functions "firstNameErrors" and "lastNameErrors" under one function named "requiredErrors" rather than having two separate functions.
Code pen: https://codepen.io/pen/jebLgM
Any help is much appreciated.
Thank you.