Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

react-hook-form shows no error when you leave a field

ErrorNo error message appears after tabbing out of an invalid field

Short answer

react-hook-form validates on submit by default, so leaving a field shows nothing. Set mode to onBlur in useForm. For MUI, Select or any custom input, use Controller and pass field.onBlur, field.onChange and field.ref to the real input. Otherwise the library never hears the blur event.

  • Applies to react-hook-form 7, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the form's validation mode and the touched fields, then focus and leave one invalid field. If errors stay empty after the blur, work through the causes in order.

    const { control, formState: { errors, touchedFields } } = useForm({ mode: "onBlur" });
    console.log(errors, touchedFields);
  2. 2Match what you see, then jump to its fix

    1. 1Errors only show after the first submitmode was never set, so the default onSubmit appliesFix
    2. 2Plain inputs validate on blur but the MUI or custom component does notA controlled input does not pass onBlur back to the formFix
    3. 3register is spread on a wrapper component, the value is undefined and the field cannot be focused on errorA custom component with register does not forward the refFix

What does the error look like?

  • No error message appears after tabbing out of an invalid field
  • Errors only appear after pressing the submit button
  • Validation works on blur for plain inputs but not for a UI-library component

Cause 1 of 3

mode was never set, so the default onSubmit applies

1Check

Open the useForm call. The default mode is onSubmit, which validates only when handleSubmit runs.

const form = useForm();            // mode is "onSubmit"

2Fix

Pass mode in the single options object of useForm. Use onTouched if you want the first check on blur and then a check on every change.

const { register, handleSubmit, formState: { errors } } = useForm({
  mode: "onBlur",          // or "onTouched"
});

3Verify

Focus the field, type something invalid and press Tab. The error should appear without submitting.

<p>{errors.email?.message}</p>

Confirmed by react-hook-form.com

Did this fix it?

Cause 2 of 3

A controlled input does not pass onBlur back to the form

1Check

Look at how the component is connected. Components that are not plain DOM inputs need Controller, and the render prop gives you field.onBlur, field.onChange, field.name and field.ref. The docs say onBlur is how the library hears that the input was interacted with.

<Controller name="email" control={control} render={({ field }) => (
  <TextField {...field} />
)} />

2Fix

Spread field onto the input, or assign each prop by hand if the component renames them. If you wrap onBlur or onChange in your own handler, call field.onBlur() and field.onChange() inside it.

<Controller
  name="email"
  control={control}
  rules={{ required: "Email is required" }}
  render={({ field, fieldState }) => (
    <TextField
      {...field}
      error={!!fieldState.error}
      helperText={fieldState.error?.message}
    />
  )}
/>

3Verify

fieldState.isTouched becomes true after the first blur, and the error shows.

console.log(fieldState.isTouched);

Confirmed by react-hook-form.com

Did this fix it?

Cause 3 of 3

A custom component with register does not forward the ref

1Check

register reads the value through the ref of the real input element. A function component that drops ref or onBlur breaks that link.

<MyInput {...register("email", { required: true })} />

2Fix

Pass all props, including ref, to the underlying input. If the component calls the prop something else, such as inputRef, map it by hand, or switch to Controller.

const { ref, ...rest } = register("email", { required: true });
<MyInput {...rest} inputRef={ref} />

3Verify

Submit an empty form: the first invalid field takes focus, and blur now validates it.

// no extra command needed

Confirmed by react-hook-form.com

Did this fix it?

How do I stop it coming back?

  • Decide the mode once for the whole form: onSubmit for short forms, onBlur or onTouched for long ones.
  • Wrap every non-DOM input in Controller and always forward onBlur, onChange and ref.
  • Do not use mode: all unless you need it; change events add re-renders.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.