Your own field type

Your own field type

Add a field type when none of the built-in ones asks the question the way you need.

When to write one

Write a field type when a question needs an input the engine does not have, and more than one form will use it. A rule about the answer is a check, not a new type.

This page builds rating: one to five stars, sent as a number.

The type

A field type is one file. It says what the field takes, how the answer starts, what makes it valid, and how it reads in the review.

// fields/rating/rating.ts
import type { FieldBase } from "#form-engine";
import { z } from "zod/v4";
import { defineFieldType } from "#form-engine";
import RatingInput from "./RatingInput.vue";

export interface RatingField extends FieldBase<"rating"> {
  max?: number;
}

declare module "#form-engine/types" {
  interface FieldTypes {
    rating: RatingField;
  }
}

export default defineFieldType<RatingField>({
  type: "rating",
  component: RatingInput,
  verb: "choose",
  width: "half",
  empty: () => null,
  schema: (field, { required }) => {
    const stars = z.number({ error: required }).int().min(1).max(field.max ?? 5);
    return field.optional ? stars.nullable() : stars;
  },
  format: (value, field) => `${value} / ${field.max ?? 5}`,
  sample: field => field.max ?? 5,
});

declare module tells TypeScript the new type exists, so a form can write type: "rating" and get its options checked.

The input

The component gets the field and the answer as v-model. Use Nuxt UI, so it takes the app's look.

<!-- fields/rating/RatingInput.vue -->
<script setup lang="ts">
import type { RatingField } from "./rating";

defineOptions({ inheritAttrs: false });

const { field } = defineProps<{ field: RatingField }>();

const model = defineModel<number | null>();
</script>

<template>
  <div class="flex gap-1" role="radiogroup">
    <UButton
      v-for="star in field.max ?? 5"
      :key="star"
      color="primary"
      variant="ghost"
      square
      role="radio"
      :aria-checked="model === star"
      :aria-label="`${star} of ${field.max ?? 5}`"
      @click="model = star"
    >
      <svg viewBox="0 0 24 24" class="size-6" :fill="star <= (model ?? 0) ? 'currentColor' : 'none'" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round">
        <path d="m12 3 2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z" />
      </svg>
    </UButton>
  </div>
</template>

Register it

Once, in a plugin, next to installFormEngine:

import { registerFieldTypes } from "#form-engine";
import rating from "~/fields/rating/rating";

export default defineNuxtPlugin(() => {
  registerFieldTypes(rating);
});

A form can now ask for it:

{ key: "service", type: "rating", label: "How was the inspection?" }
// service: 4
0/1

Payload

{}

Pick a number of stars and watch the payload below the form.

Registering a type with the name of a built-in one is an error; registering the same name twice keeps the last.

Every key

KeyWhat it is
typethe name forms write
componentthe input
verbenter, choose or attach, for the "Enter …" required message
emptythe answer before people give one
schemathe zod rule for a valid answer
formatthe answer as text, for the review
samplean answer the debugger's Fill step can use. Every type needs one.
widthhow much of a row it takes: third, half, two-thirds, full
columnthe narrowest its column in a list's table may be, in rem
serializethe answer as the API wants it, when that differs
deserializea saved answer back into the form's shape
filledwhether it counts as answered, when not empty is not enough
reviewa component for the review, instead of format's text
ownsLabelthe component draws its own label