# Form

## Specification

Component that wraps the set of fields and controls their data flow.

The key focus of a form design is getting rid of obscure configurations and achieving a clear and predictable layout. Although depending on the use case, form shares a lot of functionalities and behaviors. Unifying the latter, depriving the developer from configuring it over and over, is a primary goal of React Advanced Form.

## Props

### General

| Prop name                                                                                 | Type                                                                                         | Description                                 |
| ----------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | ------------------------------------------- |
| `ref`                                                                                     | `Function`                                                                                   | Getter function for the `<Form>` component. |
| [`innerRef`](https://redd.gitbook.io/react-advanced-form/components/form/props/inner-ref) | `Function`                                                                                   | Getter function for the `form` element.     |
| [`action`](https://redd.gitbook.io/react-advanced-form/components/form/props/action)      | `Function<Promise>`                                                                          | Submit action handler.                      |
| [`rules`](https://redd.gitbook.io/react-advanced-form/components/form/props/rules)        | [`ValidationRules`](https://redd.gitbook.io/react-advanced-form/components/broken-reference) | Form-specific validation rules.             |
| [`messages`](https://redd.gitbook.io/react-advanced-form/components/form/props/messages)  | [`ValidationMessages`](https://redd.gitbook.io/react-advanced-form/validation/messages)      | Form-specific validation messages.          |

### Callbacks

| Callback name    | Descripton                                                  |
| ---------------- | ----------------------------------------------------------- |
| `onFirstChange`  | Called when the form becomes dirty.                         |
| `onReset`        | Called after the form has been reset.                       |
| `onSubmitStart`  | Called when the submit has started (form is valid).         |
| `onSubmitted`    | Called after successful submit.                             |
| `onSubmitFailed` | Called on failed submit.                                    |
| `onSubmitEnd`    | Called when the submit has ended, regardless of its status. |
| `onInvalid`      | Called when the submit failed due to form being invalid.    |

{% hint style="info" %}
Read more about each prop and callback under [Form callbacks](https://redd.gitbook.io/react-advanced-form/components/form/callbacks).
{% endhint %}

## Methods

Form methods are available under its [reference](https://redd.gitbook.io/react-advanced-form/architecture/referencing#component-reference) object.

| Method name                                                                                  | Type                | Descripton                                 |
| -------------------------------------------------------------------------------------------- | ------------------- | ------------------------------------------ |
| [`validate`](https://redd.gitbook.io/react-advanced-form/components/form/methods/validate)   | `Function<Promise>` | Validates form manually.                   |
| [`serialize`](https://redd.gitbook.io/react-advanced-form/components/form/methods/serialize) | `Function<Object>`  | Serializes form manually.                  |
| [`reset`](https://redd.gitbook.io/react-advanced-form/components/form/methods/reset)         | `Function`          | Resets the fields to their initial values. |
| [`submit`](https://redd.gitbook.io/react-advanced-form/components/form/methods/submit)       | `Function<Promise>` | Submits the form manually.                 |

{% hint style="info" %}
Learn more about each method under [Form methods](https://redd.gitbook.io/react-advanced-form/components/form/methods).
{% endhint %}

## Example

Let's create a simple form that has a single input and an action handler.

> React Advanced Form is field-centric. That means that you may want to configure a set of [composite field components](https://redd.gitbook.io/react-advanced-form/getting-started/creating-fields) as the first step of integrating this solution into your application.

```jsx
import React from 'react'
import { Form } from 'react-advanced-form'
import { Input } from 'react-advanced-form-addons'

export default class Example extends React.Component {
  registerUser = ({ serialized, fields, form }) => {
    console.log(serialized) // { "firstName": "John" }
  }
  
  render() {
    return (
      <Form action={this.registerUser}>
        <Input
          name="firstName"
          label="First name"
          rule={/[a-zA-Z]/}
          initialValue="John"
          required />
      </Form>
    )
  }
}
```

{% hint style="info" %}
You can use [`FormProvider`](https://redd.gitbook.io/react-advanced-form/components/form-provider) to specify application-wide validation rules and messages, or you can pass them to a specific Form using [`rules`](https://redd.gitbook.io/react-advanced-form/components/form/props/rules) and [`messages`](https://redd.gitbook.io/react-advanced-form/components/form/props/messages) props respectively.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://redd.gitbook.io/react-advanced-form/components/form.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
