Form — 폼
입력값을 받고 검증하기 위한 폼 컴포넌트예요. 입력창, 라디오, 체크박스, 파일 업로드 같은 다양한 입력 타입을 지원하고, 규칙 기반 검증으로 값을 한 번에 확인할 수 있어요.
소개
데이터 입력과 검증에 사용해요. 입력창, 라디오 버튼, 체크박스, 파일 업로드 등 다양한 타입을 지원해요.
설치
import { createApp } from 'vue'
import { Form, FormItem } from '@nutui/nutui'
const app = createApp()
app.use(Form)
app.use(FormItem)
기본 사용법
demo: form basic
동적 폼
demo: form dynamic
폼 검증
4.1.0에서 Promise 기반 커스텀 에러 메시지 기능이 추가됐어요:
validator는 함수이고, 반환값 타입은 boolean | Promise예요. false를 반환하거나 resolve(false)로 이행되는 Promise를 돌려주면 검증 실패를 뜻하고, message에 설정한 안내 문구가 표시돼요. reject(error)로 거부되는 Promise를 돌려주면 그 error가 안내 문구로 사용되며, message보다 우선순위가 높아요.
demo: form validate
폼 타입
demo: form type
라벨·필수 표시(별표) 위치 커스터마이즈 v4.2.4
demo: form star
API
Form Props
| Attribute | Description | Type | Default |
|---|---|---|---|
| model-value | Form data object (required when using form verification) | object | |
| rules | Unified configuration FormItem attr rules | { prop: FormItemRule[] } | {} |
| disabled | Disable all data entry components under the form | boolean | false |
label-positionv4.2.4 |
The location of the form item label | top | left | right |
left |
star-positionv4.2.4 |
The red star position of the single label is required | left | right |
left |
Form Events
| Event | Description | Arguments |
|---|---|---|
| validate | Triggered after any single table item fails to be verified | The prop value of the form item to be verified, whether the verification is passed, and the error message (if any) |
FormItem Props
| Attribute | Description | Type | Default |
|---|---|---|---|
| required | Whether to display the red asterisk next to the label of the required field | boolean | false |
| prop | The v-model field of the form field is required when the form verification function is used | string | - |
| rules | Define validation rules | FormItemRule [] | [] |
| label-width | The width of the form item label. The default unit is px |
number | string | - |
| label-align | Form item label alignment. The optional values are center right |
string | left |
| body-align | Default Solt box alignment. The optional values are center right |
string | left |
| error-message-align | Error prompt text alignment. The optional values are center and right |
string | left |
| show-error-line | Whether to mark the input box in red when the verification fails | boolean | true |
| show-error-message | Whether to display the error prompt under the input box when the verification fails | boolean | true |
label-positionv4.2.4 |
The location of the form item label, The priority is higher than that in form label-position | top | left | right |
left |
star-positionv4.2.4 |
The red star position of the single label is required, The priority is higher than that in form star-position | left | right |
left |
FormItemRule 데이터 구조
FormItem의 rules 속성으로 검증 규칙을 정의해요. 사용할 수 있는 속성은 아래와 같아요.
| Attribute | Default | Type |
|---|---|---|
| required | Is it a required field | boolean |
| message | Error prompt copy | string |
| validator | Verification by function | (value:string,rule?:FormItemRule) => boolean | Promise |
| regex | Verification by regular expression | RegExp |
FormItem Slots
| Name | Description |
|---|---|
| default | Default slot |
| label | Custom label slot |
Methods
ref로 Form 인스턴스를 받아서 인스턴스 메서드를 호출할 수 있어요.
| Name | Description | Arguments | Return value |
|---|---|---|---|
| submit | Method of submitting form for verification | - | - |
| reset | Clear verification results | - | - |
| validate | Active trigger verification is used to trigger when the user customizes the scene, such as blur and change events | Same as FormItem prop value | - |
Types v4.3.1
컴포넌트는 아래 타입 정의를 export해요.
import type {
FormErrorMessage,
FormLabelPosition,
FormStarPosition,
FormItemRule,
FormProps,
FormInstance,
FormItemProps,
FormItemInstance
} from '@nutui/nutui';
테마 (Theming)
CSS 변수
이 컴포넌트는 스타일을 커스터마이징할 때 쓸 수 있는 CSS 변수를 제공해요. 자세한 내용은 ConfigProvider 컴포넌트 문서를 참고하세요.
| Name | Default Value |
|---|---|
| --nut-form-item-error-line-color | var(--nut-required-color) |
| --nut-form-item-required-color | var(--nut-required-color) |
| --nut-form-item-error-message-color | var(--nut-required-color) |
| --nut-form-item-label-font-size | 14px |
| --nut-form-item-label-width | 90px |
| --nut-form-item-label-margin-right | 10px |
| --nut-form-item-label-text-align | left |
| --nut-form-item-required-margin-right | 4px |
| --nut-form-item-body-font-size | 14px |
| --nut-form-item-body-slots-text-align | left |
| --nut-form-item-body-input-text-align | left |
| --nut-form-item-tip-font-size | 10px |
| --nut-form-item-tip-text-align | left |
더 알아보기 (Learn more)
이 문서는 NutUI v4 공식 영문 문서를 한국어로 옮긴 글이에요. 최신 API와 변경 사항은 원문에서 확인하는 게 가장 정확해요.
- 원문 문서: Form · doc.en-US.md
- NutUI 공식 문서: nutui.jd.com / GitHub 저장소: jd-opensource/nutui