Abdelrahman Awad
Software Engineer @Rasayel. Open-Source contributor.
FE Engineer @
Creator ofย
ee-validate
โ Vue 3 Support
โจ Better DX
โก Faster Forms
ย
<template>
<div>
<input type="text" name="email" v-model="value">
{{ errorMessage }}
</div>
</template>
<script>
import { useField } from 'vee-validate';
export default {
setup() {
const { value, errorMessage } = useField(
'email', 'required|email'
);
return {
value,
errorMessage
};
}
};
</script>
import { useField, useForm } from 'vee-validate';
export default {
setup() {
const { form, handleSubmit } = useForm();
const { value: password, errors: passwordErrors } = useField(
'password', 'required', { form }
);
const { value: email, errors: emailErrors } = useField(
'email', 'required', { form }
);
const onSubmit = handleSubmit(values => {
// Send data to the API
console.log(values);
});
return {
password,
passwordErrors,
email,
emailErrors,
onSubmit
};
}
};
<Field name="firstName" as="input" />
<Field name="email" as="input" type="email" />
<Field name="drink" as="select">
<option value="coffee">Coffee</option>
<option value="tea">Tea</option>
<option value="coke">Coke</option>
</Field>
<Field name="email" as="custom-text-input" />
<Field v-slot="{ field }">
<!-- This will be validated -->
<input v-bind="field">
<!-- This won't be validated -->
<input class="addon" type="checkbox">
</Field>
You can use `v-slot` to render more complex markup
<Form as="form" @submit="onSubmit">
<Field name="email" as="input" type="email" />
<button type="submit">
Submit
</button>
</Form>
<Form as="form" @submit="onSubmit">
<Field name="email" as="input" type="email" />
<ErrorMessage name="email" />
<button>Submit</button>
</Form>
import * as yup from 'yup';
export default {
components: {
// ...
},
setup() {
const emailSchema = yup.string()
.required()
.email();
return {
emailSchema,
// ...
};
}
};
<Form as="form" @submit="onSubmit">
<Field
name="email"
as="input"
type="email"
:rules="emailSchema"
/>
<ErrorMessage name="email" />
<button>Submit</button>
</Form>
<Form as="form" @submit="onSubmit">
<Field
name="email"
as="input"
type="email"
rules="required|email"
/>
<ErrorMessage name="email" />
<button>Submit</button>
</Form>
export default {
setup() {
// ...
const schema = yup.object().shape({
email: yup.string().required().email(),
name: yup.string().required(),
password: yup.string().required().min(8),
});
return {
schema,
// ...
}
}
};
<Form
:validation-schema="schema"
as="form"
@submit="submit"
>
<Field name="email" as="input" />
<ErrorMessage name="email" />
<Field name="name" as="input" />
<ErrorMessage name="name" />
<Field name="name" as="input" type="password" />
<ErrorMessage name="password" />
<button>Submit</button>
</Form>
You can try out vee-validate v4 with Vue 3 now
yarn add vee-validate@next
# or
npm install vee-validate@next
๐ Documentation
(Still WIP)
By Abdelrahman Awad