Skip to content

Instantly share code, notes, and snippets.

@mhaecal
Last active July 31, 2023 02:36
Show Gist options
  • Save mhaecal/e4dd79e946a30da26987901bf0a4454c to your computer and use it in GitHub Desktop.
Save mhaecal/e4dd79e946a30da26987901bf0a4454c to your computer and use it in GitHub Desktop.
Vue 3 Form Validation with Vee Validate + Yup
<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