-
I am using the react adapter and setting the current locale with the const formSchema = z.object({
name: z.string({ required_error: "Required" }),
});
export default function CreateWishList() {
const { LL } = useContext(I18nContext);
// useForm below comes from the react-hook-form library and it takes in a schema for validation of the form
const { register } = useForm({ resolver: zodResolver(formSchema) });
// Rest of component here, such as the form
} As seen from the code above, the validation error message when One way to solve it is to move the definition of the schema inside the React component: export default function CreateWishList() {
const { LL } = useContext(I18nContext);
const formSchema = z.object({
name: z.string({ required_error: LL.REQUIRED() })
});
const { register } = useForm({ resolver: zodResolver(formSchema) });
// Rest of component here, such as the form
} I find this to be a bit messy. The schema cannot be separated from the React component since it must be inside of it, and the What would be really nice is to somehow access the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @Sti2nd, import type { TranslationFunctions } from 'src/i18n/i18n-types'
const getFormSchema = (LL: TranslationFunctions) => z.object({
name: z.string().min(1, LL.required()),
});
export default function CreateWishList() {
const { LL } = useContext(I18nContext)
let register
useEffect(() => {
register = useForm({ resolver: zodResolver(getFormSchema(LL)) }).register
}, [LL])
// Rest of component here, such as the form
} I havent tested it, I just wrote it down from my mind, so maybe it isn 100% valid code and you have to tweak it a bit. I never have used Let me know if it works! |
Beta Was this translation helpful? Give feedback.
Hi @Sti2nd,
react is always a bit tricky when you want to share something outside the react-codebase.
I'm no react expert, so I don't think I can give you the "best" approach how to handle this.
I probably would use it like this:
I havent tested it, I …