Skip to content

Binding Inputs with useField ​

Binding by hand takes four bindings on every input, and each new input repeats all of them. useField reads the same node and reduces them to two.

Replacing the Bindings ​

Each node is passed to useField, and what it returns is bound with v-model and v-bind:

checkout-form.vue
vue
<script setup lang="ts">
import type { OutputOf } from '@formtree/vue'
import { useForm } from '@formtree/vue'
import { useField, useForm } from '@formtree/vue'
import { shallowRef } from 'vue'
import { checkoutForm } from './forms/checkout'

const form = useForm(checkoutForm)
const fullName = form.field('fullName') 
const email = form.field('email') 
const fullName = useField(form.field('fullName')) 
const email = useField(form.field('email')) 
const order = shallowRef<OutputOf<typeof checkoutForm>>()

const placeOrder = form.handleSubmit(checkout => {
  order.value = checkout
})
</script>

<template>
  <form novalidate @submit.prevent="placeOrder">
    <label :for="fullName.id">Name</label>
    <input
      :id="fullName.id"
      :value="fullName.state.value"
      @input="fullName.setValue(($event.target as HTMLInputElement).value)"
      @blur="fullName.blur"
    />
    <input v-model="fullName.model" v-bind="fullName.attrs" /> 
    <p>{{ fullName.errorMessage }}</p>

    <label :for="email.id">Email</label>
    <input
      :id="email.id"
      :value="email.state.value"
      @input="email.setValue(($event.target as HTMLInputElement).value)"
      @blur="email.blur"
    />
    <input v-model="email.model" v-bind="email.attrs" /> 
    <p>{{ email.errorMessage }}</p>

    <button type="submit">Place order</button>
  </form>
  <pre v-if="order">{{ order }}</pre>
</template>

model reads the value of the node and writes it with setValue, while attrs carries the id of the input and blur as its blur handler, so the inputs work as they did before.

Connecting the Error to the Input ​

attrs also carries attributes that the inputs bound by hand did not have, such as those that mark an input as invalid and point a screen reader to its error. For them to work, the element of each error needs the errorId they point to:

vue
<template>
  <form novalidate @submit.prevent="placeOrder">
    <label :for="fullName.id">Name</label>
    <input v-model="fullName.model" v-bind="fullName.attrs" />
    <p>{{ fullName.errorMessage }}</p> 
    <p :id="fullName.errorId">{{ fullName.errorMessage }}</p> 

    <label :for="email.id">Email</label>
    <input v-model="email.model" v-bind="email.attrs" />
    <p>{{ email.errorMessage }}</p> 
    <p :id="email.errorId">{{ email.errorMessage }}</p> 

    <button type="submit">Place order</button>
  </form>
  <pre v-if="order">{{ order }}</pre>
</template>

A screen reader now announces the error of an input together with the input.

Trying the Checkout ​

Live exampleTwo inputs bound with useField

The inputs behave as before. Enter an invalid email, leave the input, and inspect it. Its aria-describedby names the error below it.

vue
<script setup lang="ts">
import type { OutputOf } from '@formtree/vue'
import { useField, useForm } from '@formtree/vue'
import { shallowRef } from 'vue'
import { checkoutForm } from './forms/checkout'

const form = useForm(checkoutForm)
const fullName = useField(form.field('fullName'))
const email = useField(form.field('email'))
const order = shallowRef<OutputOf<typeof checkoutForm>>()

const placeOrder = form.handleSubmit(checkout => {
  order.value = checkout
})
</script>

<template>
  <form novalidate @submit.prevent="placeOrder">
    <label :for="fullName.id">Name</label>
    <input v-model="fullName.model" v-bind="fullName.attrs" />
    <p :id="fullName.errorId">{{ fullName.errorMessage }}</p>

    <label :for="email.id">Email</label>
    <input v-model="email.model" v-bind="email.attrs" />
    <p :id="email.errorId">{{ email.errorMessage }}</p>

    <button type="submit">Place order</button>
  </form>
  <pre v-if="order">{{ order }}</pre>
</template>
ts
import { defineGroup } from '@formtree/vue'
import { z } from 'zod'

export const checkoutForm = defineGroup({
  schema: z.object({
    fullName: z.string({ error: 'Enter your name' }).trim().min(1, {
      error: 'Enter your name',
    }),
    email: z.email({ error: 'Enter a valid email address' }),
  }),
})

Released under the MIT License.