JGuang

Form

Form · Arco Design

Preview
npm install @arco-design/web-react@2.66.18

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: Form supports three layout: horizontal (default), vertical, inline.
  • Controlled: You can set the field property in Form.Item to control the field. <ol> <li> In the controlled mode, Form.Item will automatically set value (or other attribute set by triggerPropName) and onChange (or other attribute set by trigger) properties for Form.Item's children. All data will be processed by Form. </li> <li> You cannot set defaultValue for Form.Item's children. The default value can be set by the initialValues of Form or the initialValue of Form.Item. </li> </ol>
  • Methods: In functional components, you can use Form.useForm to 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 by ref.
  • Set Error status: You can externally control the error status of form entries by using the error parameter of the setFields method.
  • Object value: By setting field to a.b.c, you will get {a:{b:{c: xx}}}.
  • Nest Form.Item: Form.Item can 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.List will render an additional DOM node to display the validation information. If you don’t need it, you can pass in noStyle=false and customize the validation information through Form.useFormState display.
  • Nest List: Provides array management for fields.
  • Custom Form: Form.Item will pass the onChange and value properties to its direct child nodes. Only after calling this onChange, can its own value be collected by Form.Item.
  • Normalize: First, when using a controlled form, the values ​​of all form items will be collected by Form when they change. If you need to process the value of the form item and store it in the Form, you can use the normalize attribute. In contrast, when the form item is rendered, the value is taken from the Form and passed to the corresponding form item as the value attribute. If the value taken from the Form needs to be converted and then passed to For form items, you can use the formatter attribute.Note: form.setFieldValue does not trigger normalize.
  • Register: Fill in the necessary information to register a new user
  • Disabled: Set disabled to the Form component to globally disable all form controls.
  • Size: The size attribute allows you to set forms of different sizes
  • Validate status: Form.Item supports customizing the form verification status and verification copy through the hasFeedbackvalidateStatus and help attributes.
  • validate: The form field can be validated through the form.validate method. You can specify to verify specific fields through parameters.
  • async validate: Customize the validator method in rules and return a Promise to achieve asynchronous validation of the form. p.s: If using lodash.debounce does not work, it is recommended to use debounce.promise, which returns a promise.
  • validate messages: The validation message prompt template can be set through the validateMessages property (2.32.0 support). example You can also set validateMessages for the global Form component in the componentConfig parameter of the ConfigProvider component.
  • 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.0 support. To get the corresponding Form instance, you need to set the ID property for the Form component.
  • useWatch: You can use Form.useWatch to monitor the changes of field values inside the form. (2.33.0 support)
  • Get Form Context: In functional components, you can use Form.useFormContext to get a form context. Facilitate the encapsulation of form controls. (version 2.33.0, isSubmitting in 2.44.0). isSubmitting is only valid when the onSubmit attribute of the form Form is triggered to be submitted by a button with type=submit. If onSubmit contains asynchronous logic inside, return a Promise.
  • useFormState: You can use Form.useFormState to monitor the changes of status inside the form. (2.44.0 support)

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

PropertyDescriptionTypeDefaultValueVersion
disabledWhether All Form item is disabledboolean--
idprefix of id attrstring--
labelAlignText alignment of label'left' | 'right'right-
layoutThe layout of Form'horizontal' | 'vertical' | 'inline'horizontal-
requiredSymbolWhether show red symbol when item is required,Set position props, you can choose to place the symbol before/after the labelboolean | { position: 'start' | 'end' }trueposition in 2.24.0
sizesize of form'mini' | 'small' | 'default' | 'large'--
colonWhether show colon after label. Priority is lower than colon in Form.Item.(ReactNode in v2.41.0)boolean | ReactNode--
classNameAdditional css classstring | string[]--
formformFormInstance<FormData, FieldValue, FieldKey>--
initialValuesDefault value of form dataPartial<FormData>--
labelColGlobal <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.ItemColProps{ span: 5, offset: 0 }-
onChangeCallback 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--
onValuesChangeCallback 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--
scrollToFirstErrorWhether scroll to first error item after validation fails. (ScrollIntoViewOptions is supported at 2.19.0)boolean | ScrollIntoViewOptions--
styleAdditional styleCSSProperties--
validateMessagesvalidation prompt template demoPartial<{[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
validateTriggerWhen to trigger verification.string | string[]onChange2.28.0
wrapperCustom outer tag. Can be html tags or React componentsComponentTypeform-
wrapperColThe global control layout, which is the same as the setting method of labelCol,will be overwritten by the wrapperCol set by Form.ItemColProps{ span: 19, offset: 0 }-
wrapperPropsIf set wrapper, You can pass some parameters to the wrapper.IndexedObject--
onSubmitCallback when submit data(values: FormData) => void--
onSubmitFailedCallback when validate fail(errors: { [key: string]: FieldError }) => void-2.21.0

Form.Item

PropertyDescriptionTypeDefaultValueVersion
disabledWhether the FormItem is disabled. Priority is higher than the disabled prop of Formboolean--
hasFeedbackWhether to show the verification icon, configure validateStatus to use.boolean--
hiddenhide the form itemboolean-2.29.0
requiredWhether 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 rulesboolean--
triggerWhen to take over and collecting the child nodes.stringonChange-
triggerPropNameThe attribute name of the child node being taken over, default is value, ex, <Checkbox> is checked.stringvalue-
labelAlignText alignment of label'left' | 'right'right-
layoutThe layout'horizontal' | 'vertical' | 'inline'--
requiredSymbolWhether show red symbol when item is required,Set position props, you can choose to place the symbol before/after the labelboolean | { position: 'start' | 'end' }trueposition in 2.24.0
validateStatusValidate status'success' | 'warning' | 'error' | 'validating'--
colonWhether show colon after label. Priority is lower than colon in Form.Item.(ReactNode in v2.41.0)boolean | ReactNode--
extraAdditional hint content.ReactNode--
helpCustom help textReactNode--
labelLabel textReactNode--
childrenReactNode type and Fuction type childrenReact.ReactNode | FormItemChildrenFn<FormData, FieldValue, FieldKey>--
classNameAdditional css classstring | string[]--
dependenciesthe 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 shouldUpdatestring[]-2.40.0
fieldUnique identification of controlled componentsFieldKey--
initialValueDefault valueFieldValue--
labelColThe 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 settingColProps--
normalizeConvert the value to the FormItem(value: FieldValue | undefined,prevValue: FieldValue | undefined,allValues: Partial<FormData>) => any--
noStyleNo 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 nodeboolean | { showErrorTip: boolean }--
rulesValidation rules in controlled component, RulesPropsRulesProps<FieldValue>[]--
shouldUpdateWhether 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)--
styleAdditional styleCSSProperties--
tooltipTooltip textReactNode | (TooltipProps & { icon?: ReactElement })-2.43.0
validateTriggerWhen 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 calledstring | string[]onChange-
wrapperColThe control layout, which is the same as the setting method of labelCol, which will override the global wrapperCol settingColProps--
formatterConvert the value of the FormItem to children;(value: FieldValue | undefined) => any-2.23.0
getValueFromEventSpecify 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

PropertyDescriptionTypeDefaultValueVersion
noStyleFormItemProps['noStyle'].Defaults to false when rules exists (requires rendering validation information), otherwise defaults to trueFormItemProps['noStyle']-2.46.0
fieldField nameFieldKey (Required)--
initialValueDefault valueSubFieldValue[]-2.22.0
rulesValidation rules in controlled component, RulesPropsRulesProps<SubFieldValue[]>[]-2.46.0
childrenFunction 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)

PropertyDescriptionTypeDefaultValue
onFormSubmitThis method will be called when any wrapped Form component triggers a submit(id: string | undefined,values,{forms,}: {forms: {[key: string]: FormInstance;};}) => void-
onFormValuesChangeThis 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>
PropertyDescriptionTypeDefaultVersion
validateVerified and Get the Form values and Errors, If fields are not set, all fields will be verified. Support callback and promiseFunction(fields?: string[], callback(errors, values) => void)
submitsubmit the formFunction
setFieldValueSet the value of a form fieldFunction(field: string, value)
setFieldsSet the value of a group of form fields and errors.Function({ [field]: string: { value: any, error: FieldError } })
setFieldsValueSet the value of multiple form fieldsFunction({[field]: string, value})
getFieldValueGet 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 changeFunction(field: string)
getFieldsGets 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 changeFunction
getFieldsValueGet 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 changeFunction(fields: string[])
getFieldErrorGet the error status of a form fieldFunction(field: string)
getFieldsErrorGet 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[])
scrollToFieldScroll to the specified form field position. ScrollIntoViewOptionsFunction(field: string, options?: ScrollIntoViewOptions)
getTouchedFieldsGet the touched field() => string[]
resetFieldsReset the value of the Form to the initial valueFunction(field?: string[])
clearFieldsClear the value of the Form.Item and validate statusFunction(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 in Form.Item according to the actual situation. control attribute}
  • Similar to triggerPropName, by default FormItem listens to the onChange callback of child components to collect control values and trigger validation logic. If the callback of the subcomponent is not onChange, remember to set trigger={form control event callback} of FormItem
  • 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} to Form.Item.