Skip to content

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

It includes all kinds of input items, such as input, select, radio, checkbox, date-picker, time-picker, and switch.

Use the items prop to define form structure in a configuration-driven way, or use bk-form-item components to define form items manually. Each form item supports configuration options such as prop, label, and related field attributes.

Activity name
Activity zone
please select your zone
Activity time
-
Instant delivery
Activity type
Resources
Activity form
Activity name
Activity zone
please select your zone
Activity time
-
Instant delivery
Activity type
Resources
Activity form

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

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.

Approved by
Activity zone
Activity zone
Activity time

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.

Form Align
Form Item Align
Name
Activity zone
Activity form

Validation

Form component allows you to verify your data, helping you find and correct errors.

Just add the rules attribute to the Form component and provide validation rules. Each form item should define a prop field in the items configuration(Or the bk-form-item component) to bind validation to the corresponding model field. See more information at async-validator.

Activity name
Activity zone
Activity zone
Activity time
-
Instant delivery
Activity type
Resources
Activity form

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.

Email
Domain0

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.

age

TIP

When an bk-form-item is nested in another bk-form-item, its label width will be 0. You can set label-width on that bk-form-item if needed.

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.


Activity name
Activity zone
please select your zone
Activity time
-
Instant delivery
Activity type
Resources

Accessibility

When only a single input (or related control such as select or checkbox) is inside of a bk-form-item, the form item's label will automatically be attached to that input. However, if multiple inputs are inside of the bk-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.

Full Name
Your Information

Form API

Form Attributes

NameDescriptionTypeDefault
itemsConfiguration array for form items, each item can specify type, componentProps, and childrenarray
colNumNumber of columns for multi-column form layoutnumber1
Element Plus Form Attributes
NameDescriptionTypeDefault
modelData of form component.object
rulesValidation rules of form.object
inlineWhether the form is inline. (It is recommended to use the col-num attribute)booleanfalse
label-positionPosition of label. If set to 'left' or 'right', label-width prop is also required.enumright
label-widthWidth of label, e.g. '50px'. All its direct child form items will inherit this value. auto is supported.string / number''
label-suffixSuffix of the label.string''
hide-required-asteriskWhether to hide required fields should have a red asterisk (star) beside their labels.booleanfalse
require-asterisk-positionPosition of asterisk.enumleft
show-messageWhether to show the error message.booleantrue
inline-messageWhether to display the error message inline with the form item.booleanfalse
status-iconWhether to display an icon indicating the validation result.booleanfalse
validate-on-rule-changeWhether to trigger validation when the rules prop is changed.booleantrue
sizeControl the size of components in this form.enum
disabledWhether to disable all components in this form. If set to true, it will override the disabled prop of the inner component.booleanfalse
scroll-to-errorWhen validation fails, scroll to the first error form entry.booleanfalse
scroll-into-view-optionsWhen validation fails, it scrolls to the first error item based on the scrollIntoView option. scrollIntoView.object / booleantrue

Form Events

Element Plus Form Events
NameDescriptionType
validatetriggers after a form item is validatedFunction

Form Slots

Element Plus Form Slots
NameDescriptionSubtags
defaultcustomize default contentFormItem

Form Exposes

Element Plus Form Exposes
NameDescriptionType
validateValidate the whole form. Receives a callback or returns Promise.Function
validateFieldValidate specified fields.Function
resetFieldsReset specified fields and remove validation result.Function
scrollToFieldScroll to the specified fields.Function
clearValidateClear validation messages for all or specified fields.Function
fieldsGet all fields context.array
getFieldGet a field context.Function

FormItem API

FormItem Attributes

NameDescriptionTypeDefault
type requiredType of the form item component (e.g., 'input', 'select', 'radio')string
componentPropsAdditional props to pass to the underlying componentobject
childrenNested form items for complex layoutsarray
Element Plus FormItem Attributes
NameDescriptionTypeDefault
propA 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[]
labelLabel text.string
label-positionPosition of item label. If set to 'left' or 'right', label-width prop is also required. Default extend label-position of form.enum''
label-widthWidth of label, e.g. '50px'. 'auto' is supported.string / number
requiredWhether the field is required or not, will be determined by validation rules if omitted.boolean
rulesValidation rules of form, see the following table, more advanced usage at async-validator.object
errorField error message, set its value and the field will validate error and show this message immediately.string
show-messageWhether to show the error message.booleantrue
inline-messageInline style validate message.booleanfalse
sizeControl the size of components in this form-item.enum
forSame as for in native label.string
validate-statusValidation state of formItem.enum

FormItemRule

Element Plus FormItemRule
NameDescriptionTypeDefault
triggerHow 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
NameDescriptionType
defaultContent of Form Item.
labelCustom content to display on label.object
errorCustom content to display validation message.object

FormItem Exposes

Element Plus FormItem Exposes
NameDescriptionType
sizeForm item size.object
validateMessageValidation message.object
validateStateValidation state.object
validateValidate form item.Function
resetFieldReset current field and remove validation result.Function
clearValidateRemove validation status of the field.Function

Released under the MIT License.