<script setup lang="ts">
import { useForm } from '@tanstack/vue-form'
import { type } from 'arktype'
import * as v from 'valibot'
import { z } from 'zod'
import { Schema as S } from 'effect'
import FieldInfo from './FieldInfo.vue'
const ZodSchema = z.object({
firstName: z
.string()
.min(3, '[Zod] You must have a length of at least 3')
.startsWith('A', "[Zod] First name must start with 'A'"),
lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
})
const ValibotSchema = v.object({
firstName: v.pipe(
v.string(),
v.minLength(3, '[Valibot] You must have a length of at least 3'),
v.startsWith('A', "[Valibot] First name must start with 'A'"),
),
lastName: v.pipe(
v.string(),
v.minLength(3, '[Valibot] You must have a length of at least 3'),
),
})
const ArkTypeSchema = type({
firstName: 'string >= 3',
lastName: 'string >= 3',
})
const EffectSchema = S.standardSchemaV1(
S.Struct({
firstName: S.String.pipe(
S.minLength(3),
S.annotations({
message: () => '[Effect/Schema] You must have a length of at least 3',
}),
),
lastName: S.String.pipe(
S.minLength(3),
S.annotations({
message: () => '[Effect/Schema] You must have a length of at least 3',
}),
),
}),
)
const form = useForm({
defaultValues: {
firstName: '',
lastName: '',
},
validators: {
// DEMO: You can switch between schemas seamlessly
onChange: ZodSchema,
// onChange: ValibotSchema,
// onChange: ArkTypeSchema,
// onChange: EffectSchema,
},
onSubmit: async ({ value }) => {
// Do something with form data
alert(JSON.stringify(value))
},
})
</script>
<template>
<form
@submit="
(e) => {
e.preventDefault()
e.stopPropagation()
form.handleSubmit()
}
"
>
<div>
<form.Field name="firstName">
<template v-slot="{ field, state }">
<label :htmlFor="field.name">First Name:</label>
<input
:id="field.name"
:name="field.name"
:value="field.state.value"
@input="
(e) => field.handleChange((e.target as HTMLInputElement).value)
"
@blur="field.handleBlur"
/>
<FieldInfo :state="state" />
</template>
</form.Field>
</div>
<div>
<form.Field name="lastName">
<template v-slot="{ field, state }">
<label :htmlFor="field.name">Last Name:</label>
<input
:id="field.name"
:name="field.name"
:value="field.state.value"
@input="
(e) => field.handleChange((e.target as HTMLInputElement).value)
"
@blur="field.handleBlur"
/>
<FieldInfo :state="state" />
</template>
</form.Field>
</div>
<form.Subscribe>
<template v-slot="{ canSubmit, isSubmitting }">
<button type="submit" :disabled="!canSubmit">
{{ isSubmitting ? '...' : 'Submit' }}
</button>
</template>
</form.Subscribe>
</form>
</template>
<script setup lang="ts">
import { useForm } from '@tanstack/vue-form'
import { type } from 'arktype'
import * as v from 'valibot'
import { z } from 'zod'
import { Schema as S } from 'effect'
import FieldInfo from './FieldInfo.vue'
const ZodSchema = z.object({
firstName: z
.string()
.min(3, '[Zod] You must have a length of at least 3')
.startsWith('A', "[Zod] First name must start with 'A'"),
lastName: z.string().min(3, '[Zod] You must have a length of at least 3'),
})
const ValibotSchema = v.object({
firstName: v.pipe(
v.string(),
v.minLength(3, '[Valibot] You must have a length of at least 3'),
v.startsWith('A', "[Valibot] First name must start with 'A'"),
),
lastName: v.pipe(
v.string(),
v.minLength(3, '[Valibot] You must have a length of at least 3'),
),
})
const ArkTypeSchema = type({
firstName: 'string >= 3',
lastName: 'string >= 3',
})
const EffectSchema = S.standardSchemaV1(
S.Struct({
firstName: S.String.pipe(
S.minLength(3),
S.annotations({
message: () => '[Effect/Schema] You must have a length of at least 3',
}),
),
lastName: S.String.pipe(
S.minLength(3),
S.annotations({
message: () => '[Effect/Schema] You must have a length of at least 3',
}),
),
}),
)
const form = useForm({
defaultValues: {
firstName: '',
lastName: '',
},
validators: {
// DEMO: You can switch between schemas seamlessly
onChange: ZodSchema,
// onChange: ValibotSchema,
// onChange: ArkTypeSchema,
// onChange: EffectSchema,
},
onSubmit: async ({ value }) => {
// Do something with form data
alert(JSON.stringify(value))
},
})
</script>
<template>
<form
@submit="
(e) => {
e.preventDefault()
e.stopPropagation()
form.handleSubmit()
}
"
>
<div>
<form.Field name="firstName">
<template v-slot="{ field, state }">
<label :htmlFor="field.name">First Name:</label>
<input
:id="field.name"
:name="field.name"
:value="field.state.value"
@input="
(e) => field.handleChange((e.target as HTMLInputElement).value)
"
@blur="field.handleBlur"
/>
<FieldInfo :state="state" />
</template>
</form.Field>
</div>
<div>
<form.Field name="lastName">
<template v-slot="{ field, state }">
<label :htmlFor="field.name">Last Name:</label>
<input
:id="field.name"
:name="field.name"
:value="field.state.value"
@input="
(e) => field.handleChange((e.target as HTMLInputElement).value)
"
@blur="field.handleBlur"
/>
<FieldInfo :state="state" />
</template>
</form.Field>
</div>
<form.Subscribe>
<template v-slot="{ canSubmit, isSubmitting }">
<button type="submit" :disabled="!canSubmit">
{{ isSubmitting ? '...' : 'Submit' }}
</button>
</template>
</form.Subscribe>
</form>
</template>
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.