You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hai, I'm quite new to Vue and coding in general. How do I handle asynchronous server validation on handleChange/onBlur.
I saw the example using the Field method but I need to use composition API instead of the component. Also is there any way to add handleChange for other input without giving me an error?
const SIGNUP_SCHEMA = {
email: value => {
if (!value) return 'This field is required'
const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (!regex.test(String(value).toLowerCase())) {
return 'Please enter a valid email'
}
return true
}
}
useForm({
validationSchema: SIGNUP_SCHEMA
})
const { value: email, errorMessage: emailError, handleChange } = useField('email', {
validateOnValueUpdate: false,
});
const validationListeners = computed(() => {
if (!emailError.value) {
return {
blur: handleChange,
change: handleChange,
input: e => handleChange(e, false),
}
}
return {
blur: handleChange,
change: handleChange,
input: handleChange,
}
})
Below is what I really want but without using Field since I was required to use our own component
<template>
<Form @submit="onSubmit">
<Field
id="email"
name="email"
:rules="validateEmail"
type="email"
/>
<ErrorMessage name="email" />
<button @click="checkForm">
Test
</button>
</Form>
</template>
<script setup>
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
import { Field, Form, ErrorMessage } from "vee-validate"
const store = useStore()
async function validateEmail(value) {
if (!value) return 'This feild is needed'
const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
if (!regex.test(String(value).toLowerCase())) {
return 'Please enter a valid email'
}
const response = await store.dispatch('checkUnique', {
type: 'email',
value: value
})
if (response) {
return 'This email is taken'
}
return true
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hai, I'm quite new to Vue and coding in general. How do I handle asynchronous server validation on handleChange/onBlur.
I saw the example using the Field method but I need to use composition API instead of the component. Also is there any way to add handleChange for other input without giving me an error?
Below is what I really want but without using Field since I was required to use our own component
Beta Was this translation helpful? Give feedback.
All reactions