Form
A form with data collection, verification and submission functions, including checkboxes, radio buttons, input boxes, drop-down selection boxes and other elements.
Install
npm install @arco-design/web-react@2.66.18
Import @arco-design/web-react/dist/css/arco.css at the entry and set the locale with ConfigProvider. React 19 projects must also import @arco-design/web-react/es/_util/react-19-adapter first, or imperative APIs such as Message and Modal cannot render.
Examples
- Basic: Usage of uncontrolled mode.
- Layout:
Formsupports three layout: horizontal (default), vertical, inline. - Controlled: You can set the
fieldproperty inForm.Itemto control the field. <ol> <li> In the controlled mode,Form.Itemwill automatically setvalue(or other attribute set bytriggerPropName) andonChange(or other attribute set bytrigger) properties for Form.Item's children. All data will be processed byForm. </li> <li> You cannot setdefaultValuefor Form.Item's children. The default value can be set by theinitialValuesofFormor theinitialValueofForm.Item. </li> </ol> - Methods: In functional components, you can use
Form.useFormto get a form instance, You can call all form methods through this instance, such as setting form value, reset form, etc. If you are using class component, you can get it byref. - Set Error status: You can externally control the error status of form entries by using the
errorparameter of thesetFieldsmethod. - Object value: By setting
fieldtoa.b.c, you will get{a:{b:{c: xx}}}. - Nest Form.Item:
Form.Itemcan be nested. - shouldUpdate: Dynamically change the field of the form by
shouldUpdate. - List: Manage the form structure of array type through
Form.List. When setting validation rules,Form.Listwill render an additionalDOMnode to display the validation information. If you don’t need it, you can pass innoStyle=falseand customize the validation information throughForm.useFormStatedisplay. - Nest List: Provides array management for fields.
- Custom Form:
Form.Itemwill pass theonChangeandvalueproperties to its direct child nodes. Only after calling thisonChange, can its own value be collected byForm.Item. - Normalize: First, when using a controlled form, the values of all form items will be collected by
Formwhen they change. If you need to process the value of the form item and store it in theForm, you can use thenormalizeattribute. In contrast, when the form item is rendered, the value is taken from theFormand passed to the corresponding form item as thevalueattribute. If the value taken from theFormneeds to be converted and then passed to For form items, you can use theformatterattribute.Note: form.setFieldValue does not trigger normalize. - Register: Fill in the necessary information to register a new user
- Disabled: Set
disabledto theFormcomponent to globally disable all form controls. - Size: The
sizeattribute allows you to set forms of different sizes - Validate status:
Form.Itemsupports customizing the form verification status and verification copy through the hasFeedbackvalidateStatusandhelpattributes. - validate: The form field can be validated through the
form.validatemethod. You can specify to verify specific fields through parameters. - async validate: Customize the
validatormethod inrulesand return aPromiseto achieve asynchronous validation of the form. p.s: If usinglodash.debouncedoes not work, it is recommended to usedebounce.promise, which returns apromise. - validate messages: The validation message prompt template can be set through the
validateMessagesproperty (2.32.0support). example You can also setvalidateMessagesfor the globalFormcomponent in thecomponentConfigparameter of theConfigProvidercomponent. - Scroll To Field
- validate level: You can use 'validateLevel' to set form validation failures to 'warning' and not block form submission.
- Manage multiple form data: The 'form.provider' component allows you to manage data from multiple forms.
2.30.0support. To get the corresponding Form instance, you need to set the ID property for the Form component. - useWatch: You can use
Form.useWatchto monitor the changes of field values inside the form. (2.33.0support) - Get Form Context: In functional components, you can use
Form.useFormContextto get a form context. Facilitate the encapsulation of form controls. (version2.33.0,isSubmittingin2.44.0).isSubmittingis only valid when theonSubmitattribute of the formFormis triggered to be submitted by abuttonwithtype=submit. IfonSubmitcontains asynchronous logic inside, return a Promise. - useFormState: You can use
Form.useFormStateto monitor the changes of status inside the form. (2.44.0support)
Examples come from the official Arco Design repository (MIT) at the commit published as npm 2.66.18. Some use online images or upload endpoints; network requests are disabled in previews.
API
Form
| Property | Description | Type | DefaultValue | Version |
|---|---|---|---|---|
| disabled | Whether All Form item is disabled | boolean | - | - |
| id | prefix of id attr | string | - | - |
| labelAlign | Text alignment of label | 'left' | 'right' | right | - |
| layout | The layout of Form | 'horizontal' | 'vertical' | 'inline' | horizontal | - |
| requiredSymbol | Whether show red symbol when item is required,Set position props, you can choose to place the symbol before/after the label | boolean | { position: 'start' | 'end' } | true | position in 2.24.0 |
| size | size of form | 'mini' | 'small' | 'default' | 'large' | - | - |
| colon | Whether show colon after label. Priority is lower than colon in Form.Item.(ReactNode in v2.41.0) | boolean | ReactNode | - | - |
| className | Additional css class | string | string[] | - | - |
| form | form | FormInstance<FormData, FieldValue, FieldKey> | - | - |
| initialValues | Default value of form data | Partial<FormData> | - | - |
| labelCol | Global <label> label layout. Same as the props received by the <Grid.Col>,the values of span and offset can be configured,which will be overwritten by the labelCol set by Form.Item | ColProps | { span: 5, offset: 0 } | - |
| onChange | Callback when the form item value changes. Unlike onValuesChange, it will only be called when the user manipulates the form item | (value: Partial<FormData>, values: Partial<FormData>) => void | - | - |
| onValuesChange | Callback when any form item value changes.The first is the changed value, and the second is the value of all items | (value: Partial<FormData>, values: Partial<FormData>) => void | - | - |
| scrollToFirstError | Whether scroll to first error item after validation fails. (ScrollIntoViewOptions is supported at 2.19.0) | boolean | ScrollIntoViewOptions | - | - |
| style | Additional style | CSSProperties | - | - |
| validateMessages | validation prompt template demo | Partial<{[key in keyof ValidateMessagesTemplateType]: ValidateMessagesTemplateType[key] extends string? ValidateMessagesTemplateType[key] | ((data, { label }) => any): Partial<Record<keyof ValidateMessagesTemplateType[key], string | ((data, { label }) => any)>>;}> | - | 2.32.0 |
| validateTrigger | When to trigger verification. | string | string[] | onChange | 2.28.0 |
| wrapper | Custom outer tag. Can be html tags or React components | ComponentType | form | - |
| wrapperCol | The global control layout, which is the same as the setting method of labelCol,will be overwritten by the wrapperCol set by Form.Item | ColProps | { span: 19, offset: 0 } | - |
| wrapperProps | If set wrapper, You can pass some parameters to the wrapper. | IndexedObject | - | - |
| onSubmit | Callback when submit data | (values: FormData) => void | - | - |
| onSubmitFailed | Callback when validate fail | (errors: { [key: string]: FieldError }) => void | - | 2.21.0 |
Form.Item
| Property | Description | Type | DefaultValue | Version |
|---|---|---|---|---|
| disabled | Whether the FormItem is disabled. Priority is higher than the disabled prop of Form | boolean | - | - |
| hasFeedback | Whether to show the verification icon, configure validateStatus to use. | boolean | - | - |
| hidden | hide the form item | boolean | - | 2.29.0 |
| required | Whether the FormItem is Required, Will display an red symbol in front of the label label.If it is not set here, it will look for required from the rules | boolean | - | - |
| trigger | When to take over and collecting the child nodes. | string | onChange | - |
| triggerPropName | The attribute name of the child node being taken over, default is value, ex, <Checkbox> is checked. | string | value | - |
| labelAlign | Text alignment of label | 'left' | 'right' | right | - |
| layout | The layout | 'horizontal' | 'vertical' | 'inline' | - | - |
| requiredSymbol | Whether show red symbol when item is required,Set position props, you can choose to place the symbol before/after the label | boolean | { position: 'start' | 'end' } | true | position in 2.24.0 |
| validateStatus | Validate status | 'success' | 'warning' | 'error' | 'validating' | - | - |
| colon | Whether show colon after label. Priority is lower than colon in Form.Item.(ReactNode in v2.41.0) | boolean | ReactNode | - | - |
| extra | Additional hint content. | ReactNode | - | - |
| help | Custom help text | ReactNode | - | - |
| label | Label text | ReactNode | - | - |
| children | ReactNode type and Fuction type children | React.ReactNode | FormItemChildrenFn<FormData, FieldValue, FieldKey> | - | - |
| className | Additional css class | string | string[] | - | - |
| dependencies | the dependency fields. When the value of the dependent field changes, trigger its own validation.If you want to dynamically render a form control/form area, use shouldUpdate | string[] | - | 2.40.0 |
| field | Unique identification of controlled components | FieldKey | - | - |
| initialValue | Default value | FieldValue | - | - |
| labelCol | The layout of <label>, the same as the props received by the <Grid.Col>.The values of span and offset can be configured, which will override the global labelCol setting | ColProps | - | - |
| normalize | Convert the value to the FormItem | (value: FieldValue | undefined,prevValue: FieldValue | undefined,allValues: Partial<FormData>) => any | - | - |
| noStyle | No external tags/styles are rendered, only binding field. Notice: When set to true, if the field verification failed,the error message will not be displayed. You can pass in an object and set showErrorTip to true(Support at 2.5.0),The error message will be displayed under the upper formItem node | boolean | { showErrorTip: boolean } | - | - |
| rules | Validation rules in controlled component, RulesProps | RulesProps<FieldValue>[] | - | - |
| shouldUpdate | Whether to re-render when other FormItem value change. When set to true, any changes to the Form will re-render. | | boolean| ((prevValues: Partial<FormData>,currentValues: Partial<FormData>,info: {isFormList?: boolean;field?: FieldKey | FieldKey[];isInner?: boolean;}) => boolean) | - | - |
| style | Additional style | CSSProperties | - | - |
| tooltip | Tooltip text | ReactNode | (TooltipProps & { icon?: ReactElement }) | - | 2.43.0 |
| validateTrigger | When to trigger verification. The value is related to the wrapped item, and all events supported.For example, onFocus, onBlur, and onChange supported by Input can be used as the value of validateTrigger.When passed as [], the validation rules will only be executed when the form validate method is called | string | string[] | onChange | - |
| wrapperCol | The control layout, which is the same as the setting method of labelCol, which will override the global wrapperCol setting | ColProps | - | - |
| formatter | Convert the value of the FormItem to children; | (value: FieldValue | undefined) => any | - | 2.23.0 |
| getValueFromEvent | Specify how to handle the value when the child node triggers the onChange event. (If the trigger attribute is customized, then the parameter here is the parameter type of the corresponding event callback function) | (...args) => FieldValue | - | 2.23.0 |
Form.List
| Property | Description | Type | DefaultValue | Version |
|---|---|---|---|---|
| noStyle | FormItemProps['noStyle'].Defaults to false when rules exists (requires rendering validation information), otherwise defaults to true | FormItemProps['noStyle'] | - | 2.46.0 |
| field | Field name | FieldKey (Required) | - | - |
| initialValue | Default value | SubFieldValue[] | - | 2.22.0 |
| rules | Validation rules in controlled component, RulesProps | RulesProps<SubFieldValue[]>[] | - | 2.46.0 |
| children | Function type children | (fields: { key: number; field: SubFieldKey }[],operation: {add: (defaultValue?: SubFieldValue, index?: number) => void;remove: (index: number) => void;move: (fromIndex: number, toIndex: number) => void;}) => React.ReactNode | - | - |
Form.Provider(2.30.0)
| Property | Description | Type | DefaultValue |
|---|---|---|---|
| onFormSubmit | This method will be called when any wrapped Form component triggers a submit | (id: string | undefined,values,{forms,}: {forms: {[key: string]: FormInstance;};}) => void | - |
| onFormValuesChange | This method is called when the value of any wrapped Form component changes | (id: string | undefined,changedValues,{forms,}: {forms: {[key: string]: FormInstance;};}) => void | - |
FormInstance
export type FormInstance<
FormData = any,
FieldValue = FormData[keyof FormData],
FieldKey extends KeyType = keyof FormData
> = Pick<
Store<FormData, FieldValue, FieldKey>,
| "getFieldsValue"
| "getFieldValue"
| "getFieldError"
| "getFieldsError"
| "getTouchedFields"
| "getFields"
| "setFieldValue"
| "setFieldsValue"
| "setFields"
| "resetFields"
| "clearFields"
| "submit"
| "validate"
| "getFieldsState"
> & {
scrollToField: (field: FieldKey, options?: ScrollIntoViewOptions) => void;
// arco 内部使用,业务万不可调用
getInnerMethods: (
inner?: boolean
) => InnerMethodsReturnType<FormData, FieldValue, FieldKey>;
};
InnerMethodsReturnType
export type InnerMethodsReturnType<
FormData = any,
FieldValue = FormData[keyof FormData],
FieldKey extends KeyType = keyof FormData
> = Pick<
Store<FormData, FieldValue, FieldKey>,
| "registerField"
| "registerStateWatcher"
| "registerFormWatcher"
| "registerWatcher"
| "innerSetInitialValues"
| "innerSetInitialValue"
| "innerSetCallbacks"
| "innerSetFieldValue"
| "innerGetStore"
| "innerGetStoreStatus"
| "innerCollectFormState"
| "innerGetFieldValue"
>;
ComponentType
export type ComponentType =
| keyof JSX.IntrinsicElements
| React.ComponentType<any>;
IndexedObject
export type IndexedObject = {
[key: string]: any;
};
FieldError
export type FieldError<FieldValue = any> = {
value?: FieldValue;
message?: ReactNode;
type?: string;
requiredError?: boolean;
};
FormItemChildrenFn
export type FormItemChildrenFn<
FormData = any,
FieldValue = FormData[keyof FormData],
FieldKey extends KeyType = keyof FormData
> = (
formData: any,
form: FormInstance<FormData, FieldValue, FieldKey>
) => React.ReactNode;
Rules
Low-level use b-validate.
export interface RulesProps {
// when to validate
validateTrigger?: string | string[];
// When the verification fails, the error message will be displayed in the form of `error` or `warning`. Will not block form submission when set to `warning`
validateLevel?: 'error' | 'warning';
required?: boolean;
type?: string;
length?: number;
// Array
maxLength?: number;
minLength?: number;
includes?: boolean;
deepEqual?: any;
empty?: boolean;
// Number
min?: number;
max?: number;
equal?: number;
positive?: boolean;
negative?: boolean;
// Object
hasKeys?: string[];
// String
match?: RegExp;
uppercase?: boolean;
lowercase?: boolean;
// Boolean
true?: boolean;
false?: boolean;
// custom
validator?: (value, callback: (error?: ReactNode) => void) => void;
message?: string;
}
this.form
You can get this.form through ref, and this.form contains common operations such as form validation and setting.
<Form ref={(ref) => (this.form = ref)}>
<FormItem>...</FormItem>
</Form>
| Property | Description | Type | Default | Version |
|---|---|---|---|---|
| validate | Verified and Get the Form values and Errors, If fields are not set, all fields will be verified. Support callback and promise | Function(fields?: string[], callback(errors, values) => void) | ||
| submit | submit the form | Function | ||
| setFieldValue | Set the value of a form field | Function(field: string, value) | ||
| setFields | Set the value of a group of form fields and errors. | Function({ [field]: string: { value: any, error: FieldError } }) | ||
| setFieldsValue | Set the value of multiple form fields | Function({[field]: string, value}) | ||
| getFieldValue | Get the field value of a form field.Please do not use the return value directly as a dependency of useEffect, the return value will be deeply cloned and the reference address will change | Function(field: string) | ||
| getFields | Gets the values of all form items, including those that were created and then destroyed.Please do not use the return value directly as a dependency of useEffect, the return value will be deeply cloned and the reference address will change | Function | ||
| getFieldsValue | Get the field value of a group of form fields. If you don't set fields, you will get the field value of all fields.Please do not use the return value directly as a dependency of useEffect, the return value will be deeply cloned and the reference address will change | Function(fields: string[]) | ||
| getFieldError | Get the error status of a form field | Function(field: string) | ||
| getFieldsError | Get the error status of a group of form fields. If you don't set fields, you will get the error status of all fields. | Function(fields?: string[]) | ||
| scrollToField | Scroll to the specified form field position. ScrollIntoViewOptions | Function(field: string, options?: ScrollIntoViewOptions) | ||
| getTouchedFields | Get the touched field | () => string[] | ||
| resetFields | Reset the value of the Form to the initial value | Function(field?: string[]) | ||
| clearFields | Clear the value of the Form.Item and validate status | Function(field?: string[]) | 2.29.0 |
validate Usage
this.form.validate((errors, values) => {
console.log(errors, values);
});
// Or
this.form.validate().then((values) => {
console.log(values);
}).catch((error) => {
console.log(error.name);
console.log(error.message);
console.log(error.errors);
});
// Or (Note to use in the async method)
try {
const values = await this.form.validate();
} catch (error) {
console.log(error.name);
console.log(error.message);
console.log(error.errors);
}
setFields Usage
this.form.setFields({
name: {
value: 'pjy',
error: {
message: 'Yes, I know!'
},
warning: 'warning...'
}
});
Common Problems
How to set the default value of Form?(Why doesn't setting defaultValue directly to Input control take effect)
Do not set the defaultValue and value attributes on the control wrapped by the Form.Item with the field attribute set. You can set the default value on the Form.Item through the initialValue or the initialValues property of the Form.
Why can't call the method of Form nested in Modal?
For example, the following usage:
// ...
const [form] = Form.useForm();
useEffect(() => {
form.setFieldsValue({})
}, [])
return <div>
<Modal visible={visible}>
<Form form={form}>
{/** ... */}
</Form>
</Modal>
</div>
This is because when the form method is called, Modal has not yet been mounted and Form has not been created yet. You can set the mountOnEnter=false of Modal.
Form.Item did not collect the value of the wrapped form control / did not trigger verification?
- Form.Item sets the field attribute
- Form.Item is directly wrapped outside the form control, and the form control is the only child node of FormItem
- The default triggerPropName of Form.Item is value, which means that the value attribute will be injected into the child control. If the controlled property of the form control is
checked(such as switch, checkbox, radio),fileList(such as Upload) or others, then you need to set the triggerPropName={ of the form control inForm.Itemaccording to the actual situation. control attribute} - Similar to triggerPropName, by default
FormItemlistens to theonChangecallback of child components to collect control values and trigger validation logic. If the callback of the subcomponent is notonChange, remember to settrigger={form control event callback}ofFormItem - The control wrapped by Form.Item is in a mounted state (that is, it has not been destroyed)
A form element has a value filled in but has been destroyed. How do I get this value?
The getFields method will obtain the values of all fields registered in Form, including form items that are created and destroyed.
Verification throttling or asynchronous verification?
Customize the validator method in rules and return a Promise to implement asynchronous verification of the form.
If throttling with lodash.debounce does not take effect, it is recommended to use debounce.promise, which returns a promise.
[Official website example](https://arco.design/react/components/form#%E8%A1%A8%E5%8D%95%E5%BC%82%E6%AD%A5%E6%A0%A1% E9%AA%8C)
The Checkbox/Switch/Upload settings in Form do not take effect.
Set triggerPropName=checked on the Form.Item corresponding to Checkbox/Switch
Set triggerPropName=fileList on the Form.Item corresponding to Upload
Other components are similar
Does the Input in the Form appear with a light blue background color (Chrome browser)?
The blue background color is the automatic filling style of the Chrome browser. Just turn off the automatic filling of the form.
How to turn off autofill?
The native property of Form component, just set autoComplete=off.
Will clicking the label trigger a change in the behavior of the corresponding form control? For example: switch, checkbox selected, input focused
https://www.runoob.com/tags/tag-label.html Label definition and usage instructions The label tag defines the label (marker) for the input element. The label element does not present any special effects to the user. However, it improves usability for mouse users. This control is triggered if you click on the text inside the label element. That is to say, when the user selects the label, the browser will automatically turn the focus to the form control related to the label. label The for attribute of the label should be the same as the id attribute of the related element. If you really don't want this effect, just set an id attribute to the form control, and don't match it with the for attribute of the label. Such as
<Switch id="aaaaa" />
Advanced customization of error message display
https://codepen.io/yinkaihui/pen/GRwRQao?editors=1011
DatePicker.WeekPicker value cannot be parsed
Same as DatePicker.QuarterPicker Add
getValueFromEvent={(v, dv) => dv}toForm.Item.