Form
Form component enhanced based on Element Plus Form.
Form consists of input, radio, select, checkbox and so on. With form, you can collect, verify and submit data.
TIP
The component has been upgraded with a flex layout to replace the old float layout.
Basic Usage
The most basic form includes various input form items, such as input, select, radio, checkbox, etc.
In each Form component, you need to use the bk-form-item component as the container for input items. You can also define the form structure in a configuration-driven manner through the items property. Each form item supports configuring options such as prop, label, and related field attributes.
TIP
W3C regulates that
When there is only one single-line text input field in a form, the user agent should accept Enter in that field as a request to submit the form.
To prevent this behavior, you can add @submit.prevent on <bk-form>.
Multi-Column Form @items
When the vertical space is limited and the form is relatively simple, you can put it in one line.
Set the col-num attribute to specify the number of columns for the form layout.
Alignment
Depending on your design, there are several different ways to align your label element.
You can set the label-position separately in items. If the value is empty, the label-position of bk-form is used.
The label-position attribute decides how labels align, it can be top or left. When set to top, labels will be placed at the top of the form field.
Validation
Form component allows you to verify your data, helping you find and correct errors.
You only need to add the rules attribute to the bk-form component and pass in the validation rules, then set the prop attribute of the form item to the specific key value that needs to be validated. For validation rules, refer to async-validator.
Custom Validation Rules
This example shows how to customize your own validation rules to finish a two-factor password verification.
Here we use status-icon to reflect validation result as an icon.
TIP
Custom validate callback function must be called. See more advanced usage at async-validator.
Add/Delete Form Item
In addition to passing all validation rules at once on the form component, you can also pass the validation rules or delete rules on a single form field dynamically.
Number Validate
Number Validate need a .number modifier added on the input v-model binding,it's used to transform the string value to the number which is provided by Vue.
TIP
When a form item is nested within another form item, its label width is set to 0. If necessary, you can manually set the label-width attribute on the nested form item.
Size Control
All components in a Form inherit their size attribute from that Form. Similarly, FormItem also has a size attribute.
Still you can fine tune each component's size if you don't want that component to inherit its size from From or FormItem.
Accessibility
When only a single input (or related control such as select or checkbox) is inside of a form item, the form item's label will automatically be attached to that input. However, if multiple inputs are inside of the form item, the form item will be assigned the WAI-ARIA role of group instead. In this case, it is your responsibility to assign assistive labels to the individual inputs.
Form API
Form Attributes
| Name | Description | Type | Default |
|---|---|---|---|
| items | Configuration array for form items, each item can specify type, componentProps, and children | array | — |
| colNum | Number of columns for multi-column form layout | number | 1 |
Element Plus Form Attributes
| Name | Description | Type | Default |
|---|---|---|---|
| model | Data of form component. | object | — |
| rules | Validation rules of form. | object | — |
| inline | Whether the form is inline. (It is recommended to use the col-num attribute) | boolean | false |
| label-position | Position of label. If set to 'left' or 'right', label-width prop is also required. | enum | right |
| label-width | Width of label, e.g. '50px'. All its direct child form items will inherit this value. auto is supported. | string / number | '' |
| label-suffix | Suffix of the label. | string | '' |
| hide-required-asterisk | Whether to hide required fields should have a red asterisk (star) beside their labels. | boolean | false |
| require-asterisk-position | Position of asterisk. | enum | left |
| show-message | Whether to show the error message. | boolean | true |
| inline-message | Whether to display the error message inline with the form item. | boolean | false |
| status-icon | Whether to display an icon indicating the validation result. | boolean | false |
| validate-on-rule-change | Whether to trigger validation when the rules prop is changed. | boolean | true |
| size | Control the size of components in this form. | enum | — |
| disabled | Whether to disable all components in this form. If set to true, it will override the disabled prop of the inner component. | boolean | false |
| scroll-to-error | When validation fails, scroll to the first error form entry. | boolean | false |
| scroll-into-view-options | When validation fails, it scrolls to the first error item based on the scrollIntoView option. scrollIntoView. | object / boolean | true |
FormItem
Extends all Element Plus FormItemProps properties with additional features:
| Name | Description | Type | Default |
|---|---|---|---|
| type required | Type of the form item component (e.g., 'input', 'select', 'radio') | string | — |
| componentProps | Additional props to pass to the underlying component | object | — |
| children | Nested form items for complex layouts | array | — |
Form Events
Element Plus Form Events
| Name | Description | Type |
|---|---|---|
| validate | triggers after a form item is validated | Function |
Form Slots
Element Plus Form Slots
| Name | Description | Subtags |
|---|---|---|
| default | customize default content | FormItem |
Form Exposes
Element Plus Form Exposes
| Name | Description | Type |
|---|---|---|
| validate | Validate the whole form. Receives a callback or returns Promise. | Function |
| validateField | Validate specified fields. | Function |
| resetFields | Reset specified fields and remove validation result. | Function |
| scrollToField | Scroll to the specified fields. | Function |
| clearValidate | Clear validation messages for all or specified fields. | Function |
| fields | Get all fields context. | array |
| getField | Get a field context. | Function |
FormItem API
FormItem Attributes
Element Plus FormItem Attributes
| Name | Description | Type | Default |
|---|---|---|---|
| prop | A key of model. It could be a path of the property (e.g a.b.0 or ['a', 'b', '0']). In the use of validate and resetFields method, the attribute is required. | string / string[] | — |
| label | Label text. | string | — |
| label-position | Position of item label. If set to 'left' or 'right', label-width prop is also required. Default extend label-position of form. | enum | '' |
| label-width | Width of label, e.g. '50px'. 'auto' is supported. | string / number | — |
| required | Whether the field is required or not, will be determined by validation rules if omitted. | boolean | — |
| rules | Validation rules of form, see the following table, more advanced usage at async-validator. | object | — |
| error | Field error message, set its value and the field will validate error and show this message immediately. | string | — |
| show-message | Whether to show the error message. | boolean | true |
| inline-message | Inline style validate message. | boolean | false |
| size | Control the size of components in this form-item. | enum | — |
| for | Same as for in native label. | string | — |
| validate-status | Validation state of formItem. | enum | — |
FormItemRule
Element Plus FormItemRule
| Name | Description | Type | Default |
|---|---|---|---|
| trigger | How the validator is triggered. | enum | — |
TIP
If you don't want to trigger the validator based on input events, set the validate-event attribute as false on the corresponding input type components (<el-input>, <el-radio>, <el-select>, ...).
FormItem Slots
Element Plus FormItem Slots
| Name | Description | Type |
|---|---|---|
| default | Content of Form Item. | — |
| label | Custom content to display on label. | object |
| error | Custom content to display validation message. | object |
FormItem Exposes
Element Plus FormItem Exposes
| Name | Description | Type |
|---|---|---|
| size | Form item size. | object |
| validateMessage | Validation message. | object |
| validateState | Validation state. | object |
| validate | Validate form item. | Function |
| resetField | Reset current field and remove validation result. | Function |
| clearValidate | Remove validation status of the field. | Function |