Fields
A field is a single value with rules of its own.
Defining a Field
A value that needs rules of its own gets a definition of its own, as Choosing Between a Key and a Child explains. defineField defines it, and a group adds it to its children:
export const priceField = defineField({
schema: z.number({ error: 'Price is required' }).positive({
error: 'Price must be positive',
}),
})
export const productForm = defineGroup({
schema: z.object({
name: z.string({ error: 'Name is required' }).trim().min(1, {
error: 'Name is required',
}),
}),
children: {
price: priceField,
},
})Every form that uses priceField validates the price with the same rule. DefineFieldOptions lists what else a field definition can hold.
Starting Empty
A field starts as undefined, never as 0 or an empty string:
const price = form.field('price')
const value = price.state.value
The schema determines whether an empty value is an error.
Changing a Value from Code
Input bindings write to a field on their own. A value from anywhere else, such as a button that picks a price, goes through setValue, and blur does what leaving an input does:
const price = form.field('price')
price.setValue(25)
price.blur()Using a Field as a Form
Any definition can be the root of a form, a field included. A form made from priceField edits only the price:
<script setup lang="ts">
import type { Product } from './api'
import { useForm } from '@formtree/vue'
import { updatePrice } from './api'
import { priceField } from './forms/product'
const props = defineProps<{ product: Product }>()
const priceForm = useForm(priceField, { initialValue: props.product.price })
const savePrice = priceForm.handleSubmit(async price => {
await updatePrice(props.product.id, price)
})
</script>import type { Product } from './api'
import { useForm } from '@formtree/react'
import { updatePrice } from './api'
import { priceField } from './forms/product'
export function PriceCell({ product }: { product: Product }) {
const priceForm = useForm(priceField, { initialValue: product.price })
const savePrice = priceForm.handleSubmit(async price => {
await updatePrice(product.id, price)
})
}import type { Product } from './api'
import { createForm } from '@formtree/core'
import { updatePrice } from './api'
import { priceField } from './forms/product'
export function createPriceForm(product: Product) {
const priceForm = createForm(priceField, { initialValue: product.price })
const savePrice = priceForm.handleSubmit(async price => {
await updatePrice(product.id, price)
})
return { priceForm, savePrice }
}The value of the form is the price itself, so savePrice receives a number. Inline Price Editing shows a table of these forms.
Defining a Field with Dependencies
A field that reads dependencies, such as a minimum length that depends on a role, is defined with defineFieldWithDeps and the type of the data:
export const passwordField = defineFieldWithDeps<{ minLength: number }>()({
schema: ({ deps }) =>
z.string({ error: 'Password is required' }).min(deps.minLength, {
error: `Use at least ${deps.minLength} characters`,
}),
})Every function in the options receives deps, and the group that holds the field passes them with childDeps, as Groups shows.