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
Payload
{}
Registering a type with the name of a built-in one is an error; registering the same name twice keeps the last.
Every key
| Key | What it is |
|---|---|
type | the name forms write |
component | the input |
verb | enter, choose or attach, for the "Enter …" required message |
empty | the answer before people give one |
schema | the zod rule for a valid answer |
format | the answer as text, for the review |
sample | an answer the debugger's Fill step can use. Every type needs one. |
width | how much of a row it takes: third, half, two-thirds, full |
column | the narrowest its column in a list's table may be, in rem |
serialize | the answer as the API wants it, when that differs |
deserialize | a saved answer back into the form's shape |
filled | whether it counts as answered, when not empty is not enough |
review | a component for the review, instead of format's text |
ownsLabel | the component draws its own label |