exeform
  • Getting Started
  • Motivation
  • How It Works
  • Guides
    • Validation
    • Nested Structures
  • API
    • Form
    • Field
    • <Form />
    • useForm()
    • useField()
    • useCheckboxField()
    • useFieldValue()
    • useFormIsValid()
    • useFormContext()
Powered by GitBook
On this page

Was this helpful?

  1. Guides

Validation

Validation runs in the process of updating the values. Validate is a function that returns an object with errors, where the key is represented in a flat form.

Example

// Without errors
const validate = (values) => ({});

// For simple values
const validate = (values) => {
  const errors = {};

  if (!values.email) {
    errors.email = 'This field is required';
  }

  if (!values.password) {
    errors.password = 'This field is required';
  }

  return errors;
};

// For nested values
const validate = (values) => {
  const errors = {};

  if (!values.author.firstName) {
    errors['author.firstName'] = 'This field is required';
  }

  if (!values.comments[1].text) {
    errors['comments[1].text'] = 'This field is required';
  }

  return errors;
};
PreviousHow It WorksNextNested Structures

Last updated 3 years ago

Was this helpful?