Carlos Dubón
BlogCreatingContactUsesResume
HomeBlogCreatingContactUsesResumeSnippetsGuestbook

Sep 15, 2025

triggerForm

snippet

Validates all fields and marks them as touched in a React Hook Form

import {FieldValues, UseFormReturn, Path} from 'react-hook-form';
 
export async function triggerForm<T extends FieldValues>(
  form: UseFormReturn<T>,
): Promise<boolean> {
  for (const [key, value] of Object.entries(form.getValues())) {
    const name = key as Path<T>;
 
    form.setValue(name, value ?? '', {
      shouldTouch: true,
    });
  }
 
  return form.trigger();
}
 
// Usage
 
const form = useForm(...);
 
triggerForm(form);

If you need to validate multiple forms use triggerForms instead. This function wraps triggerForm

import { UseFormReturn } from "react-hook-form";
import { triggerForm } from "./triggerForm";
 
/**
 * Triggers validation for multiple forms with different Schemas
 * @returns true if all form validation pass
 */
export async function triggerForms(forms: UseFormReturn<any>[]): Promise<boolean> {
  const results = await Promise.all(forms.map((form) => triggerForm(form)));
  return results.every((result) => result);
}
 
// Usage
 
const creditCardForm = useForm(...);
const shippingForm = useForm(...);
 
triggerForms(creditCardForm, shippingForm);

Enjoy my content? Stay updated with my newsletter.

No spam, just useful updates. Read privacy policy.
Carlos Dubón
Carlos Dubón
Software Engineer
© 2024-present Carlos Dubón. All Rights Reserved.

General

  • Home
  • Creating
  • Contact
  • Resume

Writing

  • Blog
  • Snippets
  • Guestbook
  • Uses

What I’m listening to

Not playing
Spotify