AutoForm

A form automatically created based simply on a zod schema. Perfect for simple forms that don't need custom UI. Also perfect for testing endpoints rapidly in a Typesafe manner.

form

Demos

Create or Update (new feature ✨)

<AutoForm /> automatically detects when an object is provided, allowing it to be used for both creating and updating data. This means you can seamlessly create a new object or update an existing one using the same AutoForm component. Here is an example with a simple user:

Same Autoform

User example:

{
  "username": "Maxime",
  "email": "awesome@email.com"
}

Basics

This is an example of what you can do with <AutoForm />.

Your favourite number between 1 and 10.

I agree to the .

We need your birthday to send you a gift.

Array Example

You can use arrays in your schemas to create dynamic forms.

{}

Array Primitives

<AutoForm /> also provides additional support for Array primitives (tags and multi-select).

{}

Confirm Password

Dependencies

Setting this below 18 will require parents consent.

Setting this to true will remove non-vegetarian food options.

Sub Object

Input Without Label

API

API Example

The form select options are fetched from an API.

Loading...

Controlled

About

The original idea of the <AutoForm /> component comes from the auto-form by vantezzen.

Credit to vozmarkov for the implementation of Array Primitives.

We've added some features (mode, resetOnSubmit, noValidate, etc.) and simplified the code. Enjoy!

Installation

Run the following command:

npx ever-ui-clx@0.2.1 add demo-auto-form

Usage

import { z } from "zod";
import { Button } from "@/components/ui/button";
import { AutoForm } from "@/registry/default/ui/auto-form";
const SCHEMA_DEMO = z.object({
  string: z.string().min(4, {
    message: "String must be at least 4 characters.",
  }),
  number: z.coerce.number().refine((val) => val >= 42, {
    message: "Number must be at least 42 !.",
  }),
});
type TSchema_Demo = z.infer<typeof SCHEMA_DEMO>;
<AutoForm
  formSchema={SCHEMA_DEMO}
  onSubmit={(data) => handleSubmit(data)}
  mode="onBlur" // optionnal
  // resetOnSubmit={false}
>
  <Button type="submit">Submit</Button>
</AutoForm>