Form
A set of components and models that process form data.
<Form>Define a form.<FormControl>Define form-control.<FormGroup>Define form groups, used for form layout.<ControlLabel>title of form-control.<HelpBlock>help infomation of form-controll
Usage
import { Form, FormGroup, FormControl, ControlLabel, HelpBlock } from 'rsuite';Layouts
Default
The default is the vertical layout
Fluid
The fluid property allows the Input 100% of the form to fill the container, valid only in vertical layouts.
Horizontal layout
Inline Layout
Layout In Modal
Status
Help Block
<HelpBlock> A help description can be defined below the form component. If the tooltip property is set, an icon will be displayed on the form component and the help description information will be displayed as <Tooltip>.
Error Message
Error message can be set in 2 ways:
- The
<FormControl>component passes anerrorMessageproperty setting error message, anderrorPlacementsets the location of the error message display. - Customize a prompt message.
Disabled and read only
Validations
Default check
The form will automatically trigger the data check after the submit event is triggered.
Schema Model
Form Check needs to be used <Form>, <FormControl> and Schema.Model 。
<Form>To define a form, you can setvalueandmodelfor the form, andmodelis the data model created bySchema.Model.<FormControl>Define a Filed that corresponds to thekeyof theSchema.Modelobject via thenameproperty. For detailed reference: Custom Form Components.Schema.ModelDefine a data model, using the reference schema.- Custom trigger check:
<Form>instance providescheckandcheckForFieldmethods, used to trigger form checksum field validation
Asynchronous check
Under certain conditions, we need to perform asynchronous verification on the data, such as verifying whether the username is duplicated. The following example will illustrate the processing of asynchronous verification.
- Set the
checkAsyncproperty on<FormControl>that requires asynchronous validation. - The validation rules for asynchronous validation add an object with a return value of Promise via the
addRulemethod ofschema. - The check can be triggered manually by calling
checkAsyncandcheckForFieldAsyncof<Form>.
Custom FormControl
All Data Entry-related components can be used in forms such as Checkbox, SelectPicker, Slider, and so on. But you need to use the FormControl component for data management and data association with the Form component.
FormControlused to bind data fields in a Form, passing thenameattribute to thekeyof the Schema.Model object.FormControlthe default is anInputcomponent, which can be set through the ʻaccepter` component.
For example:
<FormControl accepter={CheckboxGroup} />, FormControl renders a<CheckboxGroup>component and binds to the Schema.Model instance in the Form. The rich text editor in the following example, using react-quill
Third-Party Libraries
Take text-mask as an example:
Custom trigger verification
In some cases, there is no need for real-time validation of the form data. You can customize the way the control is validated and configure the checkTrigger parameter.
The default value of checkTrigger is 'change', options includes:
'change': trigger verification when data change'blur': trigger verification when component blur'none': Only valid when calling thecheck()method of<Form>
There are checkTrigger properties on the <Form> and <FormControl> components. You can define the entire form's validation method in <Form>. If there is a form component that needs to handle the validation independently, you can Set it on <FormControl>.
You can also set the check delay time
checkDelay, the default value is500milliseconds.
Props
<Form>
| Property | Type (default) |
Description |
|---|---|---|
| checkDelay | number (500) |
Delayed processing when data check, unit: millisecond |
| checkTrigger | enum: 'change','blur','none' ('change') |
Trigger the type of form validation |
| classPrefix | string ('form') |
The prefix of the component CSS class |
| errorFromContext | boolean (true) |
Error reminders in FormControl are defaulted from Context |
| fluid | boolean | The fluid property allows the Input 100% of the form to fill the container, valid only in vertical layouts |
| formDefaultValue | Object | Default value of form |
| formError | Object | Error message of form |
| formValue | Object | Value of form (Controlled) |
| layout | enum: 'horizontal', 'vertical', 'inline' ('vertical') |
Set the left and right columns of the layout of the elements within the form |
| model | Schema | SchemaModel Object |
| onChange | (formValue:Object, event:Object) => void | Callback fired when data changing |
| onCheck | (formError:Object) => void | Callback fired when data cheking |
| onError | (formError:Object) => void | Callback fired when error checking |
Form methods
- check
Verify form data.
check: (callback?: (formError: E) => void) => boolean;- checkAsync
Asynchronously check form data
checkAsync: () => Promise<any>;- checkForField
Checklist single field value.
checkForField: (
fieldName: keyof T,
callback?: (checkResult: CheckResult<errorMsg>) => void
) => boolean;- checkForFieldAsync
Asynchronous check form single field value
checkForFieldAsync: (fieldName: keyof T) => Promise<CheckResult>;- cleanErrors
Clean error message.
cleanErrors(callback: () => void) => void- cleanErrorForFiled
Clear single field error message
cleanErrorForFiled: (fieldName: keyof E, callback?: () => void) => void;<FormControl>
| Property | Type(default) |
Description |
|---|---|---|
| accepter | React.ElementType (Input) |
Proxied components |
| checkAsync | boolean | Asynchronous check value |
| checkTrigger | enum: 'change','blur','none' | The data validation trigger type, and it wiill overrides the setting on <Form> |
| classPrefix | string ('form-control') |
The prefix of the component CSS class |
| errorMessage | React.Node | Show error messages |
| errorPlacement | enum: Placement8 ('bottomStart') |
The placement of error messages |
| name * | string | The name of form-control |
| readOnly | boolean | Make the control readonly |
| plaintext | boolean | Make the control plaintext |
<FormGroup>
| Property | Type(default) |
Description |
|---|---|---|
| classPrefix | string ('form-group') |
The prefix of the component CSS class |
| controlId | string | Sets id for controlled component |
<ControlLabel>
| Property | Type(default) |
Description |
|---|---|---|
| classPrefix | string ('control-label') |
The prefix of the component CSS class |
| htmlFor | string | Attribute of the html label tag, defaults to the controlId of the FormGroup |
| srOnly | boolean | Screen reader only |
<HelpBlock>
| Property | Type(default) |
Description |
|---|---|---|
| classPrefix | string ('help-block') |
The prefix of the component CSS class |
| htmlFor | string | Attribute of the html label tag, defaults to the controlId of the FormGroup |
| tooltip | boolean | Whether to show through the Tooltip component |