Trigger vs formState/useFormState #12889
Replies: 2 comments
-
https://codesandbox.io/s/solitary-river-g5ztxx You would likely utilize that for manual validations, such as in a Form Wizard, where there's no need to create multiple forms or useform for every steps; instead, you focus solely on the fields within the current step and using only one form. |
Beta Was this translation helpful? Give feedback.
-
const schema = z.object({
id: z.string().min(1),
firstName: z.string().min(1),
lastName: z.string().min(1)
});
type FormSchema = z.input<typeof schema>;
const people = [{
id: "1",
firstName: "First1",
lastName: ""
},
{
id: "2",
firstName: "First2",
lastName: "Last2"
}
]
const defaultValues: FormSchema = {
id: "",
firstName: "",
lastName: ""
}
function Form() {
const form = useForm({
resolver: zodResolver(schema),
defaultValues
});
async function idChanged(user: FormSchema) {
form.setValue("firstName", user.firstName);
form.setValue("lastName", user.lastName);
await form.trigger("firstName");
await form.trigger("lastName");
}
return (
<>
<SelectInput form={form} name = "id" options={people} label = "ID" onChange={idChanged} />
<TextInput form={form} name="firstName" label="First Name" />
<TextInput form={form} name="lastName" label="Last Name" />
</>
);
} When a user selects the option with id 1, the first name will be set to |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In which situations is the
trigger()
API useful.isValid
fromformState/useFormState
is always returned , when do u need to trigger the validation manually?Beta Was this translation helpful? Give feedback.
All reactions