Last active
July 31, 2023 02:36
-
-
Save mhaecal/e4dd79e946a30da26987901bf0a4454c to your computer and use it in GitHub Desktop.
Vue 3 Form Validation with Vee Validate + Yup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script setup lang="ts"> | |
import { Form, Field, ErrorMessage } from 'vee-validate' | |
import * as yup from 'yup' | |
const schema = yup.object().shape({ | |
email: yup | |
.string() | |
.email('Email tidak valid') | |
.required((data) => `${data.path} harus diisi`), | |
password: yup | |
.string() | |
.min(5, (data) => `Password minimal ${data.min} karakter`) | |
.required((data) => `${data.label} harus diisi`) | |
.label('Password'), | |
}) | |
const onSubmit = (values: Record<string, any>) => { | |
console.log(values) | |
} | |
</script> | |
<template> | |
<Form @submit="onSubmit" :validation-schema="schema"> | |
<Field name="email" placeholder="email" /> | |
<ErrorMessage name="email" /> | |
<Field name="password" placeholder="password" /> | |
<ErrorMessage name="password" /> | |
<button type="submit">Submit</button> | |
</Form> | |
</template> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment