Conditional validation not working in useFieldArray #13073
Unanswered
giorgiEconsul
asked this question in
Q&A
Replies: 1 comment
-
|
If you want to have a conditional check based on z.object({
hasNoMiddleName: z.boolean(),
middle_name: z.string()
}).superRefine((data, ctx) => {
if (!data.hasNoMiddleName) {
if (data.middle_name.length <= 1) {
ctx.addIssue({
code: "custom",
message: t('form.errors.middle_name.required'),
path: ["middle_name"],
});
} else if (!/^[A-Za-z]+$/.test(data.middle_name)) {
ctx.addIssue({
code: "custom",
message: t('form.errors.middle_name.regex'),
path: ["middle_name"],
});
}
}
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I have an issue where i use react hook form with zod and need validation that is conditional (when checkbox is checked it should not validate certain field). I have implemented this in other forms and it uses identical logic except they are not using FieldArray and is not an array. Any suggestion is appreciated (conditional field is middle_name)
This is the schema
import { z } from 'zod';
component where it is used in custom hook
How useForm is implemented in useChildrenForm custom hook
Beta Was this translation helpful? Give feedback.
All reactions