Course contentsShow
← React
Lesson 1209 of 2,39631. Forms in ReactFree lesson

Field-Level vs Form-Level Validation State

Storing validation errors alongside values and deciding granularity: per-field messages vs summary display.

Field-Level vs Form-Level Validation State

What you'll learn: How to organize validation error messages in your form state—individual field errors versus a single summary object.

Two Approaches to Storing Validation Errors

When managing form validation state alongside your form values, you face a design choice: should each field "know" about its own errors, or should all errors live in one central place?

Field-Level State Structure

Here, validation errors sit right next to each field's value:

const [formState, setFormState] = useState({
  email: { value: '', error: '' },
  password: { value: '', error: 'Must be 8+ characters' },
  confirmPassword: { value: '', error: null }
});

Analogy: Each student carries their own graded test paper with them—the feedback is tightly coupled to the work.

Form-Level State Structure

All errors live in a separate, parallel structure:

const [formState, setFormState] = useState({
  values: {
    email: '',
    password: '',
    confirmPassword: ''
  },
  errors: {
    email: '',
    password: 'Must be 8+ characters',
    confirmPassword: null
  }
});

Analogy: The teacher keeps all graded papers in a single folder—feedback is centralized and easier to clear or review as a whole.

Choosing Your Granularity

Field-level makes sense when errors are always shown inline next to their inputs. Each field is self-contained.

Form-level shines when you need to:

  • Display a summary of all errors at the top of the form
  • Clear all errors at once after submission
  • Validate relationships between fields (password matching)

Many form libraries (like React Hook Form) use form-level structures internally because they're easier to iterate over and manipulate as a collection.

Key Takeaway: Field-level validation state keeps errors co-located with values; form-level state separates them into parallel structures, making batch operations and summaries simpler to implement.