Form
A native form element that validates its fields and gives each field its errors.
<script setup lang="ts">
import { ref, shallowRef, useTemplateRef } from 'vue';
import { Button } from '@vase-ui/vue/button';
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
import { Form } from '@vase-ui/vue/form';
import styles from './index.module.css';
const form = useTemplateRef('form');
const errors = ref({});
const loading = shallowRef(false);
async function onSubmit() {
const formData = new FormData(form.value?.element ?? undefined);
const value = String(formData.get('url'));
loading.value = true;
const response = await submitForm(value);
const serverErrors = {
url: response.error,
};
errors.value = serverErrors;
loading.value = false;
}
async function submitForm(value: string) {
// Mimic a server response
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
try {
const url = new URL(value);
if (url.hostname.endsWith('example.com')) {
return { error: 'The example domain is not allowed' };
}
} catch {
return { error: 'This is not a valid URL' };
}
return { success: true };
}
</script>
<template>
<Form ref="form" :class="styles.Form" :errors="errors" @submit="onSubmit">
<Field.Root name="url" :class="styles.Field">
<Field.Label :class="styles.Label">Homepage</Field.Label>
<Input
type="url"
required
default-value="https://example.com"
placeholder="https://example.com"
pattern="https?://.*"
:class="styles.Input"
/>
<Field.Error :class="styles.Error" />
</Field.Root>
<Button type="submit" :disabled="loading" focusable-when-disabled :class="styles.Button">
Submit
</Button>
</Form>
</template>.Form {
display: flex;
flex-direction: column;
gap: 1rem;
width: 100%;
max-width: 16rem;
}
.Field {
display: flex;
flex-direction: column;
align-items: start;
gap: 0.25rem;
}
.Label {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Input {
box-sizing: border-box;
padding: 0 0.5rem;
margin: 0;
border-radius: 0;
border: 1px solid oklch(14.5% 0 0deg);
width: 100%;
height: 2rem;
font-family: inherit;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 400;
background-color: white;
color: oklch(14.5% 0 0deg);
@media (any-pointer: coarse) {
font-size: 1rem;
line-height: 1.5rem;
}
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
&::placeholder {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
&:focus {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Error {
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(50.5% 0.213 27.518deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.4% 0.191 22.216deg);
}
}
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled, [aria-disabled='true']) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not(:disabled, [aria-disabled='true']) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:is(:disabled, [aria-disabled='true']) {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { ref, shallowRef, useTemplateRef } from 'vue';
import { Button } from '@vase-ui/vue/button';
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
import { Form } from '@vase-ui/vue/form';
const form = useTemplateRef('form');
const errors = ref({});
const loading = shallowRef(false);
async function onSubmit() {
const formData = new FormData(form.value?.element ?? undefined);
const value = String(formData.get('url'));
loading.value = true;
const response = await submitForm(value);
const serverErrors = {
url: response.error,
};
errors.value = serverErrors;
loading.value = false;
}
async function submitForm(value: string) {
// Mimic a server response
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
try {
const url = new URL(value);
if (url.hostname.endsWith('example.com')) {
return { error: 'The example domain is not allowed' };
}
} catch {
return { error: 'This is not a valid URL' };
}
return { success: true };
}
</script>
<template>
<Form ref="form" class="flex w-full max-w-64 flex-col gap-4" :errors="errors" @submit="onSubmit">
<Field.Root name="url" class="flex flex-col items-start gap-1">
<Field.Label class="text-sm font-bold text-neutral-950 dark:text-white">Homepage</Field.Label>
<Input
type="url"
required
default-value="https://example.com"
placeholder="https://example.com"
pattern="https?://.*"
class="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
/>
<Field.Error class="text-sm text-red-700 dark:text-red-400" />
</Field.Root>
<Button
type="submit"
:disabled="loading"
focusable-when-disabled
class="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-disabled:not-aria-disabled:bg-neutral-100 active:not-disabled:not-aria-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white aria-disabled:border-neutral-500 aria-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-disabled:not-aria-disabled:bg-neutral-800 dark:active:not-disabled:not-aria-disabled:bg-neutral-700 dark:aria-disabled:border-neutral-400 dark:disabled:border-neutral-400 dark:aria-disabled:text-neutral-400 dark:disabled:text-neutral-400"
>
Submit
</Button>
</Form>
</template>Anatomy
Import the components and assemble their parts:
<script setup lang="ts">
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
import { Form } from '@vase-ui/vue/form';
</script>
<template>
<Form>
<Field.Root>
<Field.Label />
<Input />
<Field.Error />
</Field.Root>
</Form>
</template>Examples
Submit to a server
To submit the form to a server, send the FormData of the form in a submit listener. Give the errors of the response to v-model:errors.
The demo keeps the response in one state object. It disables the button while the request runs.
<script setup lang="ts">
import { shallowRef, useTemplateRef } from 'vue';
import { Button } from '@vase-ui/vue/button';
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
import { Form, type FormErrors } from '@vase-ui/vue/form';
import styles from './index.module.css';
interface FormState {
serverErrors?: FormErrors;
}
const form = useTemplateRef('form');
const state = shallowRef<FormState>({});
const loading = shallowRef(false);
// An edit of a field removes its error.
function updateErrors(serverErrors: FormErrors) {
state.value = { ...state.value, serverErrors };
}
async function formAction() {
const formData = new FormData(form.value?.element ?? undefined);
loading.value = true;
state.value = await submitForm(state.value, formData);
loading.value = false;
}
// On a real site, this function is a request to your server.
async function submitForm(_previousState: FormState, formData: FormData) {
// Mimic a server response
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
try {
const username = formData.get('username') as string | null;
if (username === 'admin') {
return { success: false, serverErrors: { username: "'admin' is reserved for system use" } };
}
// 50% chance the username is taken
const success = Math.random() > 0.5;
if (!success) {
return {
serverErrors: { username: `${username} is unavailable` },
};
}
} catch {
return { serverErrors: { username: 'A server error has occurred' } };
}
return {};
}
</script>
<template>
<Form
ref="form"
:errors="state.serverErrors"
:class="styles.Form"
@update:errors="updateErrors"
@submit="formAction"
>
<Field.Root name="username" :class="styles.Field">
<Field.Label :class="styles.Label">Username</Field.Label>
<Input
type="text"
autocomplete="username"
required
default-value="admin"
placeholder="e.g. alice132"
:class="styles.Input"
/>
<Field.Error :class="styles.Error" />
</Field.Root>
<Button type="submit" :disabled="loading" focusable-when-disabled :class="styles.Button">
Submit
</Button>
</Form>
</template>.Form {
display: flex;
flex-direction: column;
gap: 1rem;
width: 100%;
max-width: 16rem;
}
.Field {
display: flex;
flex-direction: column;
align-items: start;
gap: 0.25rem;
}
.Label {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Input {
box-sizing: border-box;
padding: 0 0.5rem;
margin: 0;
border-radius: 0;
border: 1px solid oklch(14.5% 0 0deg);
width: 100%;
height: 2rem;
font-family: inherit;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 400;
background-color: white;
color: oklch(14.5% 0 0deg);
@media (any-pointer: coarse) {
font-size: 1rem;
line-height: 1.5rem;
}
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
&::placeholder {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
&:focus {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Error {
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(50.5% 0.213 27.518deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.4% 0.191 22.216deg);
}
}
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled, [aria-disabled='true']) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not(:disabled, [aria-disabled='true']) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:is(:disabled, [aria-disabled='true']) {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { shallowRef, useTemplateRef } from 'vue';
import { Button } from '@vase-ui/vue/button';
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
import { Form, type FormErrors } from '@vase-ui/vue/form';
interface FormState {
serverErrors?: FormErrors;
}
const form = useTemplateRef('form');
const state = shallowRef<FormState>({});
const loading = shallowRef(false);
// An edit of a field removes its error.
function updateErrors(serverErrors: FormErrors) {
state.value = { ...state.value, serverErrors };
}
async function formAction() {
const formData = new FormData(form.value?.element ?? undefined);
loading.value = true;
state.value = await submitForm(state.value, formData);
loading.value = false;
}
// On a real site, this function is a request to your server.
async function submitForm(_previousState: FormState, formData: FormData) {
// Mimic a server response
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
try {
const username = formData.get('username') as string | null;
if (username === 'admin') {
return { success: false, serverErrors: { username: "'admin' is reserved for system use" } };
}
// 50% chance the username is taken
const success = Math.random() > 0.5;
if (!success) {
return {
serverErrors: { username: `${username} is unavailable` },
};
}
} catch {
return { serverErrors: { username: 'A server error has occurred' } };
}
return {};
}
</script>
<template>
<Form
ref="form"
:errors="state.serverErrors"
class="flex w-full max-w-64 flex-col gap-4"
@update:errors="updateErrors"
@submit="formAction"
>
<Field.Root name="username" class="flex flex-col items-start gap-1">
<Field.Label class="text-sm font-bold text-neutral-950 dark:text-white">Username</Field.Label>
<Input
type="text"
autocomplete="username"
required
default-value="admin"
placeholder="e.g. alice132"
class="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
/>
<Field.Error class="text-sm text-red-700 dark:text-red-400" />
</Field.Root>
<Button
type="submit"
:disabled="loading"
focusable-when-disabled
class="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-disabled:not-aria-disabled:bg-neutral-100 active:not-disabled:not-aria-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white aria-disabled:border-neutral-500 aria-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-disabled:not-aria-disabled:bg-neutral-800 dark:active:not-disabled:not-aria-disabled:bg-neutral-700 dark:aria-disabled:border-neutral-400 dark:disabled:border-neutral-400 dark:aria-disabled:text-neutral-400 dark:disabled:text-neutral-400"
>
Submit
</Button>
</Form>
</template>Submit form values as a JavaScript object
The second argument of submit has the values of the form in an object, by the name of each field that is not disabled. Use it to change the values before you send them, or to send them to an API. Each value has the type that its control gives. Input gives a string, Checkbox and Switch give a boolean, and RadioGroup and CheckboxGroup give their value.
<script setup lang="ts">
import { Form } from '@vase-ui/vue/form';
async function onSubmit(_event: SubmitEvent, formValues: { id: string; quantity: number }) {
const payload = {
product_id: formValues.id,
order_quantity: formValues.quantity,
};
await fetch('https://api.example.com', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
}
</script>
<template>
<Form @submit="onSubmit" />
</template>Give the second parameter of the listener the type of the values. With no type, that parameter is any.
Using with Zod
When you parse the schema with schema.safeParse(), use z.flattenError(result.error).fieldErrors to map the errors to the name of each field.
<script setup lang="ts">
import { ref } from 'vue';
import { z } from 'zod';
import { Button } from '@vase-ui/vue/button';
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
import { Form, type FormValues } from '@vase-ui/vue/form';
import styles from './index.module.css';
const schema = z.object({
name: z.string().min(1, 'Name is required'),
age: z.coerce.number('Age must be a number').positive('Age must be a positive number'),
});
async function submitForm(formValues: FormValues) {
const result = schema.safeParse(formValues);
if (!result.success) {
return {
errors: z.flattenError(result.error).fieldErrors,
};
}
return {
errors: {},
};
}
const errors = ref({});
async function onSubmit(_event: SubmitEvent, formValues: FormValues) {
const response = await submitForm(formValues);
errors.value = response.errors;
}
</script>
<template>
<Form :class="styles.Form" :errors="errors" @submit="onSubmit">
<Field.Root name="name" :class="styles.Field">
<Field.Label :class="styles.Label">Name</Field.Label>
<Input placeholder="Enter name" :class="styles.Input" />
<Field.Error :class="styles.Error" />
</Field.Root>
<Field.Root name="age" :class="styles.Field">
<Field.Label :class="styles.Label">Age</Field.Label>
<Input placeholder="Enter age" :class="styles.Input" />
<Field.Error :class="styles.Error" />
</Field.Root>
<Button type="submit" :class="styles.Button">Submit</Button>
</Form>
</template>.Form {
display: flex;
flex-direction: column;
gap: 1rem;
width: 100%;
max-width: 16rem;
}
.Field {
display: flex;
flex-direction: column;
align-items: start;
gap: 0.25rem;
}
.Label {
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 700;
color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
color: white;
}
}
.Input {
box-sizing: border-box;
padding: 0 0.5rem;
margin: 0;
border-radius: 0;
border: 1px solid oklch(14.5% 0 0deg);
width: 100%;
height: 2rem;
font-family: inherit;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 400;
background-color: white;
color: oklch(14.5% 0 0deg);
@media (any-pointer: coarse) {
font-size: 1rem;
line-height: 1.5rem;
}
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
&::placeholder {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
&:focus {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Error {
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(50.5% 0.213 27.518deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.4% 0.191 22.216deg);
}
}
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
outline: 0;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not(:disabled, [aria-disabled='true']) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not(:disabled, [aria-disabled='true']) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:is(:disabled, [aria-disabled='true']) {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}<script setup lang="ts">
import { ref } from 'vue';
import { z } from 'zod';
import { Button } from '@vase-ui/vue/button';
import { Field } from '@vase-ui/vue/field';
import { Input } from '@vase-ui/vue/input';
import { Form, type FormValues } from '@vase-ui/vue/form';
const schema = z.object({
name: z.string().min(1, 'Name is required'),
age: z.coerce.number('Age must be a number').positive('Age must be a positive number'),
});
async function submitForm(formValues: FormValues) {
const result = schema.safeParse(formValues);
if (!result.success) {
return {
errors: z.flattenError(result.error).fieldErrors,
};
}
return {
errors: {},
};
}
const errors = ref({});
async function onSubmit(_event: SubmitEvent, formValues: FormValues) {
const response = await submitForm(formValues);
errors.value = response.errors;
}
</script>
<template>
<Form class="flex w-full max-w-64 flex-col gap-4" :errors="errors" @submit="onSubmit">
<Field.Root name="name" class="flex flex-col items-start gap-1">
<Field.Label class="text-sm font-bold text-neutral-950 dark:text-white">Name</Field.Label>
<Input
placeholder="Enter name"
class="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
/>
<Field.Error class="text-sm text-red-700 dark:text-red-400" />
</Field.Root>
<Field.Root name="age" class="flex flex-col items-start gap-1">
<Field.Label class="text-sm font-bold text-neutral-950 dark:text-white">Age</Field.Label>
<Input
placeholder="Enter age"
class="h-8 w-full border border-neutral-950 bg-white dark:bg-neutral-950 px-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 placeholder:text-neutral-500 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white dark:border-white dark:text-white dark:placeholder:text-neutral-400"
/>
<Field.Error class="text-sm text-red-700 dark:text-red-400" />
</Field.Root>
<Button
type="submit"
class="flex h-8 items-center justify-center gap-2 rounded-none border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-disabled:not-aria-disabled:bg-neutral-100 active:not-disabled:not-aria-disabled:bg-neutral-200 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white aria-disabled:border-neutral-500 aria-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-disabled:not-aria-disabled:bg-neutral-800 dark:active:not-disabled:not-aria-disabled:bg-neutral-700 dark:aria-disabled:border-neutral-400 dark:disabled:border-neutral-400 dark:aria-disabled:text-neutral-400 dark:disabled:text-neutral-400"
>Submit</Button
>
</Form>
</template>API reference
Form
A native form element that validates its fields and gives each field its errors. The form has
novalidate, and a blocked submit moves the focus to the first invalid control.
Renders a <form> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
errors | FormErrors | undefined | Errors from outside the form, by the name of each field. v-model:errors uses it. After a
submit that passes, the first change of errors moves the focus to the first invalid control. | |
validationMode | FormValidationMode | undefined | 'submit' | The moments when the fields of the form validate. With 'submit', a field validates at each
submit, and at each change after the first submit. With 'blur', it validates when the focus
leaves its control. With 'change', it validates at each change. The validationMode of a
Field.Root wins. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | The content of the part. | |
element | props: Record<string, unknown>elementRef: (target: unknown) => void | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
submit | [event: SubmitEvent, values: Values] | A submit with each field valid. values has the value of each named field of the form that is
not disabled. The form prevents the native submit while it has a submit listener. With no
listener, the browser submits the form. Give @submit a function, because the form can miss a
listener that you add or remove after the mount. |
update:errors | [errors: FormErrors] | An edit or a reset removes the errors of a field. v-model:errors uses it. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLFormElement | null | The element of the part, or null while it is not mounted. |
validate | (name?: string | undefined) => boolean | Validates each field of the form, or each field with the name name. Returns true when they
are valid. It does not wait for a promise of validate. |
Differences from Base UI
Vase UI ports Form from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
Form | The submit event, with the native SubmitEvent and the values, is in place of onFormSubmit. The form prevents the native submit while it has a submit listener, as Base UI does for onFormSubmit. v-model:errors binds errors. The template ref has validate(), in place of actionsRef. The modes are 'submit', 'blur' and 'change'. The form has no state to read, so the family has no FormState. |
Added in Vase UI
| Part | Member | Why |
|---|---|---|
Form | submit event | The part emits submit after its validation. In Vue, a native listener in the attributes runs before the part, so the part declares the event. Base UI calls the native onSubmit of the consumer after its validation, and types.md has no row for a native listener. |
Form | update:errors event | v-model:errors binds the errors. An edit asks to remove the key of its field. Base UI takes the errors prop and has no event for the change. |
Behavior
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.