React Form

The Form is a powerful validation tool for React applications that helps manage form state and validation rules. It provides a comprehensive set of built-in validation rules, and customizable error messages. It allows developers to create robust forms with validation feedback, state tracking, and submission handling.

Getting started

To create a new Vite project, refer to the Vite documentation. Once that Vite project is ready to run with default settings, let's add the Syncfusion® React Form to the project.

Installing Syncfusion® React packages

To use the Form in this project, the @syncfusion/react-inputs package needs to be installed using the following command:

Adding CSS reference

In this article, the Material theme is applied using CSS styles, which are available in installed packages. The necessary Material CSS styles for the Form and its dependents were imported into the src/App.css file.

Adding Form component

To include the Form in your application, import the Form, FormField from the @syncfusion/react-inputs package in App.tsx.

Add the Form and FormField in application as shown in below code example.

Run the project

To run the project, use the following command:

The Form component serves as the main container that manages Form state, validation, and submission. Each form input must be wrapped in a FormField component to connect it with the Form validation system. The FormField acts as a bridge between your input controls and the Form component's state management, ensuring that validation rules are applied and errors are properly tracked for each field.

The following example demonstrates how to create a simple Form with validation. The example below illustrates a form with fields for full name, email, event type, and number of participants. Each field has appropriate validation rules defined in a ValidationRules object, creating a structured approach to form validation. The implementation showcases proper React state management using useState hooks to track form data and submission status.

Form Configuration

  • rules: Defines validation criteria using built-in rules like required, email, and minLength
  • onSubmit: Processes form data when successfully submitted and updates UI with submission results
  • onFormStateChange: Tracks the current Form state including values, errors, and validation status. The formState object provides access to values (current field values), errors (validation messages), allowSubmit (form validity status), and methods like onChange (updates field values) and onBlur (marks fields as touched).

Event Registration

Real-time validation

The Form allows for real-time validation feedback as users interact with form fields. By setting the validateOnChange property to true, validation will be performed on every input change, providing immediate feedback to users about the validity of their inputs.

Contact Support

Custom validation rules

The Form supports custom validation rules through the customValidator property. This functionality allows developers to implement complex validation logic beyond the built-in rules, such as password strength validation, domain-specific validation requirements, or conditional validation based on other field values.

Custom validators are defined as functions within the ValidationRules object for each field. When implementing a custom validator, the function receives the current field value as its parameter and should return an empty string when validation passes or an error message string when validation fails.

Account Security Setup

5+ characters, letters, numbers and underscores only
At least 8 characters
Contains uppercase letter
Contains lowercase letter
Contains number
Contains special character

Conditional validation

Conditional validation allows form fields and their validation rules to change based on values in other fields. This dynamic approach to validation enables creating responsive forms that adapt to user choices. The example below demonstrates an employment application form where selecting different employment types (Employee, Freelancer, or Contractor) causes specific fields to appear with appropriate validation rules for each role type.

Employment Application

Multi-step form wizard

The Form makes it easy to create step-by-step forms. You can split a large form into smaller steps and validate each step separately. Users complete one step before moving to the next one. This approach makes forms less overwhelming and improves the user experience, especially for forms with many fields.

New Account Creation

1
Personal
2
Address
3
Account

Integration with native HTML inputs

The Form works seamlessly with standard HTML input elements like <input>, <select>, and <checkbox>. This flexibility allows you to use familiar HTML elements while still getting all the validation and state management benefits of the Form system.

Donation Form

Custom error placement

The Form offers flexibility in how and where validation errors are displayed. Instead of showing errors next to each field, you can collect and display them in a centralized location, such as an error summary at the top of the Form. This approach is particularly useful for accessibility and creating clean form designs.

Feedback Survey

Built-in validation rules

The Syncfusion® React Form provides a comprehensive set of built-in validation rules that can be applied to form fields. These rules help ensure data integrity and proper format before submission.

RuleDescriptionExample
requiredThe form input element must have any input value (non-empty)Rule: required: [true, 'Field is required']
Valid Input: "a" or "1" or "-"
emailThe form input element must have a valid email formatRule: email: [true, 'Invalid email']
Valid Input: "form@syncfusion.com"
urlThe form input element must have a valid URL formatRule: url: [true, 'Invalid URL']
Valid Input: "https://syncfusion.com/"
dateThe form input element must have a valid date formatRule: date: [true, 'Invalid date']
Valid Input: "05/15/2017"
dateIsoThe form input element must have a valid ISO date format (YYYY-MM-DD)Rule: dateIso: [true, 'Invalid ISO date']
Valid Input: "2017-05-15"
numberThe form input element must have a valid number formatRule: number: [true, 'Not a number']
Valid Input: "1.0" or "1"
digitsThe form input element must contain only digit charactersRule: digits: [true, 'Digits only']
Valid Input: "1" (but "1a" is invalid)
maxLengthInput value's length must be less than or equal to the specified maximumRule: maxLength: [5, 'Too long']
Valid Input: "check" (but "checking" is invalid)
minLengthInput value's length must be greater than or equal to the specified minimumRule: minLength: [5, 'Too short']
Valid Input: "testing" (but "test" is invalid)
rangeLengthInput value's length must be between the specified rangeRule: rangeLength: [[4, 5], 'Invalid length']
Valid Input: "test" (but "key" is invalid)
rangeInput value (numeric) must be between the specified rangeRule: range: [[4, 5], 'Out of range']
Valid Input: "4" (but "6" is invalid)
maxInput value must be less than or equal to the specified maximumRule: max: [3, 'Value too high']
Valid Input: "3" (but "4" is invalid)
minInput value must be greater than or equal to the specified minimumRule: min: [4, 'Value too low']
Valid Input: "5" (but "2" is invalid)
regexInput value must match the specified regular expression patternRule: regex: [/^[A-z]+$/, 'Letters only']
Valid Input: "a" (but "1" is invalid)
creditCardThe form input element must have a valid credit card number formatRule: creditCard: [true, 'Invalid card']
Valid Input: "4111111111111111"
telThe form input element must have a valid telephone number formatRule: tel: [true, 'Invalid phone']
Valid Input: "+12345678901"
equalToValidates that a field's value equals another field's valueRule: equalTo: ['password', 'Passwords must match']
Valid Input: Used for confirmation fields