Replies: 3 comments 8 replies
-
Not sure that's the best way to approach things when you're asking for help. Anyway, if you need standard client-side validation, then look at the Constraint Validation API If you need validation with |
Beta Was this translation helpful? Give feedback.
-
FWIW we use and it works perfectly, we can achieve with a single schema front side and server validation simplified example : import { zfd } from "zod-form-data"
import { z } from "zod"
import { withZod } from "@remix-validated-form/with-zod"
// example of helpers to coerce/transform formData inputs
export const zfdIntOptional = () => zfd.numeric(z.number().int().optional())
export const zfdStringRequired = (minLength?: number | null, maxLength?: number | null, requiredMsg?: string) => zfd.text(z.string({ required_error: requiredMsg ?? `Cette donnée est obligatoire` }).min(minLength ?? 0).max(maxLength ?? 999999)).transform(v => v ? v.trim() : v)
// then we get a schema and a validator for the form
const params = {
foo : zfdIntOptional (),
bar : zfdStringRequired ()
}
const schema = zfd.formData(params)
const validator = withZod(z.object(zodParams))
// then in action we validate the payload
export const action: ActionFunction = async ({ request }) => {
const result = await input.validator.validate(request.formData())
if (result.error) {
// do what you want with this server error
}
}
// frontside validation : we passe the validator to the form in the component :
export default function MyForm() {
return <ValidatedForm
validator={validator}
defaultValues={defaultValues}
>
...
} I add that first we tried to use react-hook-form but we did not managed to make it work with remix. |
Beta Was this translation helpful? Give feedback.
-
Did you remember to put in root.tsx? |
Beta Was this translation helpful? Give feedback.
-
In my line of work it's literally always a requirement to have to create forms that have a specific error message for each individual requirement for each field, and to show that error message on that field in the UI.
HTML form doesn't support this, so it doesn't seem like there's a way to do that easily in Remix unless I'm missing something, since I don't have access to any form state.
Any tips on implementing this? I just want to be able to use a zod schema for validation on the client, and show any errors it creates while knowing which form field caused the error.
I've tried some of the existing libraries (
remix-forms
andremix-validated-form
), but their api's are either bad or they just don't actually work correctly in all cases.Beta Was this translation helpful? Give feedback.
All reactions