Skip to Content

Form (RHF Integration)

A set of components that bridge React Hook Form with accessible form markup. Provides context for field-level error display and label association.


Import

import { Form, FormField, FormItem, FormLabel, FormControl, FormDescription, FormMessage, } from "@phpcreation/frontend-components-react-nextjs-bundle/components/Form";

Component Roles

ComponentRole
FormWraps FormProvider — passes RHF context
FormFieldWraps Controller — connects a field to RHF
FormItemWrapper div with spacing
FormLabelAccessible label linked to the field
FormControlWraps the input — connects aria-describedby
FormDescriptionHelper text below the input
FormMessageValidation error message

Usage

import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Form, FormField, FormItem, FormLabel, FormControl, FormMessage, } from "..."; import { Input } from "..."; import { Button } from "..."; const schema = z.object({ name: z.string().min(2, "Name must be at least 2 characters"), }); export function ProfileForm() { const form = useForm({ resolver: zodResolver(schema) }); return ( <Form {...form}> <form onSubmit={form.handleSubmit(console.log)}> <FormField control={form.control} name="name" render={({ field }) => ( <FormItem> <FormLabel>Name</FormLabel> <FormControl> <input {...field} /> </FormControl> <FormMessage /> </FormItem> )} /> <Button type="submit">Save</Button> </form> </Form> ); }

In CRUD forms, these components are used internally by RenderFormFields. You typically won’t compose them manually unless building custom form layouts.


Last updated on