trigger: (name?: string | string[]) => void
Manually triggers form or input validation. This method is also useful when you have dependant validation (input validation depends on another input's value).
Props
Type | Description | Example |
---|---|---|
undefined | Triggers validation on all fields. | trigger() |
string | Triggers validation on a specific field value by name | trigger("yourDetails.firstName") |
string[] | Triggers validation on multiple fields by name. | trigger(["yourDetails.lastName"]) |
import React from "react"; import { useForm } from "react-hook-form"; export default function App() { const { register, trigger, errors } = useForm(); return ( <form> <input {...register("firstName", { required: true })} /> <input {...register("lastName", { required: true })} /> <button type="button" onClick={() => { trigger("lastName"); }}>Trigger</button> <button type="button" onClick={() => { trigger(["firstName", "lastName"]); }}>Trigger Multiple</button> <button type="button" onClick={() => { trigger(); }}>Trigger All</button> </form> ); }
import React from "react"; import { useForm } from "react-hook-form"; type FormInputs = { firstName: string lastName: string } export default function App() { const { register, trigger, errors } = useForm<FormInputs>(); return ( <form> <input {...register("firstName", { required: true })} /> <input {...register("lastName", { required: true })} /> <button type="button" onClick={() => { trigger("lastName"); }}>Trigger</button> <button type="button" onClick={() => { trigger(["firstName", "lastName"]); }}>Trigger Multiple</button> <button type="button" onClick={() => { trigger(); }}>Trigger All</button> </form> ); }