validateFormSchema
validateFormSchema validates form data against the entity’s Zod schema before making the API call. Returns typed validated data or throws with field-level errors.
Import
import { validateFormSchema } from "@phpcreation/frontend-crud-react-nextjs-bundle/utils/validateFormSchema";Usage
import { validateFormSchema } from "...";
const onSubmit = async (formData) => {
try {
const validated = await validateFormSchema(formData, entitySchema);
await apiPost("/entities", validated);
} catch (errors) {
// errors is a FieldError[] array
errors.forEach((e) => form.setError(e.field, { message: e.message }));
}
};Parameters
| Param | Type | Description |
|---|---|---|
data | Record<string, unknown> | Raw form values |
schema | ZodSchema | Zod validation schema |
Returns
- On success:
Promise<ValidatedData>— the parsed and typed data - On failure: throws an array of
{ field: string; message: string }objects
In generated CRUD pages, validateFormSchema is called automatically by AddEntity and EditEntity. Use it directly only in custom form handlers.
Related
Last updated on