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
Function
Getter function for the form element.
action
Function<Promise>
Submit action handler.
rules
Form-specific validation rules.
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.
Read more about each prop and callback under Form callbacks.

Methods

Form methods are available under its reference object.
Method name
Type
Descripton
validate
Function<Promise>
Validates form manually.
serialize
Function<Object>
Serializes form manually.
reset
Function
Resets the fields to their initial values.
submit
Function<Promise>
Submits the form manually.
Learn more about each method under Form methods.

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 as the first step of integrating this solution into your application.
1
import React from 'react'
2
import { Form } from 'react-advanced-form'
3
import { Input } from 'react-advanced-form-addons'
4
5
export default class Example extends React.Component {
6
registerUser = ({ serialized, fields, form }) => {
7
console.log(serialized) // { "firstName": "John" }
8
}
9
10
render() {
11
return (
12
<Form action={this.registerUser}>
13
<Input
14
name="firstName"
15
label="First name"
16
rule={/[a-zA-Z]/}
17
initialValue="John"
18
required />
19
</Form>
20
)
21
}
22
}
Copied!
You can use FormProvider to specify application-wide validation rules and messages, or you can pass them to a specific Form using rules and messages props respectively.
Last modified 3yr ago