본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Form

원문 보기 위키 갱신

Form은 데이터를 수집하고 검증하는 컴포넌트예요. 폼 아이템에 규칙(rules)을 걸어 두면 입력 값이 어긋났을 때 피드백을 바로 보여 줄 수 있죠. 숫자 타입 값을 다룰 때 규칙 작성법만 조심하면 검증 로직을 깔끔하게 유지할 수 있어요.

출처: 문서

본문

데이터를 모으고 검증하는 요소예요.

숫자 타입 값을 갖는 폼 아이템에 필수(required) 규칙을 적용하려면, 규칙 객체에 `type: 'number'`를 설정해야 해요.

Demos

inline.vue
custom-rule.vue
abnormal-warning.vue
custom-validation.vue
i18n.vue
top.vue
left.vue
item-only.vue
async.vue
disabled.vue
show-label.vue
partially-apply-rules.vue
custom-messages.vue
dynamic.vue
feedback-style.vue

API

Form Props

이름 타입 기본값 설명 버전
disabled boolean false 폼을 비활성화할지 여부예요.
inline boolean false 인라인 폼으로 표시할지 여부예요.
label-width number | string | 'auto' undefined 라벨의 너비예요. label-placement가 'left'일 때 특히 유용하고, 'auto'는 라벨 너비가 자동으로 조절된다는 뜻이에요.
label-align 'left' | 'right' - 라벨 텍스트 정렬이에요.
label-placement 'left' | 'top' 'top' 라벨의 배치예요.
model Object {} 폼 아이템 값을 가져오거나 설정할 객체예요.
rules type FormRules = { [itemValidatePath: string]: FormItemRule | Array<FormItemRule> | FormRules } {} 폼 아이템을 검증할 규칙들이에요.
show-feedback boolean true 피드백 영역을 표시할지 여부예요.
show-label boolean true 라벨을 표시할지 여부예요.
show-require-mark boolean - 폼 아이템이 필수일 때 필수 기호를 표시할지 여부예요.
require-mark-placement 'left' | 'right' | 'right-hanging' 'right' 필수 기호의 위치예요. 'right-hanging' 2.24.0
size 'small' | 'medium' | 'large' 'medium' 크기예요.
validate-messages FormValidateMessages undefined async-validator의 검증 메시지들이에요. 2.27.0

FormItemRule Type

아래 표는 규칙(rule)의 모든 props를 보여 주지 않아요. 모든 사용법을 알고 싶으면 async-validator를 참고하세요.
속성 타입 기본값 설명 버전
asyncValidator (rule: FormItemRule, value: any, callback: (error?: Error) => void) => void undefined 콜백 형태의 비동기 검증이에요.
key string undefined 이 규칙의 고유 키예요. 부분 규칙 적용에 쓸 수 있어요. 부분 규칙 적용 예제를 참고하세요.
level 'error' | 'warning' undefined 검증 수준이에요. 이미 에러가 있으면 'warning' 수준 검증은 건너뛰어져요.
message string undefined 검증이 실패했을 때 보여 줄 문구예요.
renderMessage () => VNodeChild undefined 메시지의 렌더 함수예요. 2.29.1
required boolean undefined 필수인지 여부예요.
trigger string | Array<string> undefined 트리거 타입이에요.
validator (rule: FormItemRule, value: any) => boolean | Error undefined 검증 규칙이에요.

FormValidateMessages Type

어떤 메시지를 덮어쓸 수 있는지 보려면 async-validator에 정의된 기본 메시지를 참고하세요.

FormItem Props

이름 타입 기본값 설명 버전
content-class string undefined 콘텐츠의 클래스 이름이에요. 2.43.0
content-style string | object undefined 콘텐츠의 스타일이에요. 2.43.0
feedback string undefined 폼 아이템의 피드백 메시지예요. 설정하면 규칙 기반 검증 결과를 모두 대체해요.
feedback-class string undefined 피드백의 세로 표시 위치 조정이에요. 2.38.2
feedback-style string | object undefined 피드백의 가로 표시 위치 조정이에요. 2.38.2
first boolean false 첫 번째 검증 에러 메시지만 표시할지 여부예요.
ignore-path-change boolean false 보통 path가 바뀌면 재렌더가 일어나고 naive-ui가 검증 결과를 지워요. ignore-path-change를 true로 설정하면 그 동작을 끌 수 있어요.
label string undefined 라벨이에요.
label-align 'left' | 'right' undefined 라벨 안의 텍스트 정렬이에요. 설정하지 않으면 부모 폼의 label-align을 상속해요.
label-placement 'left' | 'top' undefined 설정하지 않으면 부모 폼의 label-placement을 상속해요.
label-props LabelHTMLAttributes undefined 폼 아이템 안 라벨 요소의 HTML 속성이에요. 2.24.0
label-style CSSProperties | string undefined 라벨 스타일이에요.
label-width number | string | 'auto' undefined 설정하지 않으면 부모 폼의 label-width를 상속해요. 'auto'는 라벨 너비가 자동으로 조절된다는 뜻이에요.
path string undefined 부모 폼의 model 객체에서 쓸 경로예요.
required boolean false "필수" 기호를 표시할지 여부예요. 주의: 필수 규칙이 이 prop보다 우선순위가 높고, 이 prop은 검증에 어떤 영향도 주지 않아요. 검증은 여전히 규칙에 달려 있어요.
rule FormItemRule | Array<FormItemRule> undefined 이 폼 아이템을 검증할 규칙이에요. 부모 폼의 rules에서 rule-path로 가져온 규칙과 합쳐져요. 모든 규칙은 부모 폼에 설정하는 걸 권장해요.
rule-path string undefined 부모 폼의 규칙 객체에서 규칙을 가져올 경로예요. 설정하지 않으면 부모 폼 아이템의 path를 대신 사용해요.
show-feedback boolean true 피드백 영역을 표시할지 여부예요.
show-label boolean true 라벨을 표시할지 여부예요. 설정하지 않으면 부모 폼의 show-label을 상속해요.
show-require-mark boolean - 필수 기호를 표시할지 여부예요. 설정하지 않으면 부모 폼의 show-require-mark를 사용해요.
require-mark-placement 'left' | 'right' | 'right-hanging' 'right' 필수 기호의 위치예요. 설정하지 않으면 부모 폼의 require-mark-placement를 사용해요. 'right-hanging' 2.24.0
size 'small' | 'medium' | 'large' 'medium' 크기예요.
validation-status 'error' | 'success' | 'warning' undefined 폼 아이템의 검증 상태예요. 설정하면 규칙 기반 검증 결과를 대체해요.

FormItemGi Props

FormItem의 모든 props와 GridItem의 props를 받아요.

Form Methods

기본으로 검증은 규칙의 트리거와 무관하게 모든 규칙을 사용해요.
이름 타입 설명 버전
validate (validateCallback?: (errors: Array<FormValidationError> | undefined, extra: { warnings: Array<FormValidationError> | undefined }) => void, filter?: ((rule: FormItemRule) => boolean) | Array<string> | { paths: Array<string>, shouldRuleBeApplied: (rule: FormItemRule) => boolean }) => Promise<{ warnings: Array<FormValidationError> | undefined }> 폼을 검증해요. 필드 경로를 넘기면 일치하는 폼 아이템을 검증하고, 객체를 넘기면 폼 아이템과 규칙을 함께 필터해요. 경로는 폼 아이템의 path 값과 정확히 일치해요. 반환된 프로미스의 거부(rejection) 값 타입은 Array<FormValidationError>예요. warnings 2.37.1, path filter 2.45.0
restoreValidation () => void 검증 상태를 복원해요.
invalidateLabelWidth () => void 모든 폼 아이템의 라벨 너비를 다시 계산해요. label-width="auto"를 쓰면서 라벨 내용이 동적으로 바뀔 때 유용해요. NEXT_VESION

FormItem, FormItemGi Methods

이름 타입 설명 버전
validate (options: { trigger?: string, callback?: (errors: FormValidationError | undefined, extra: { warnings: FormValidationError | undefined }) => void, shouldRuleBeApplied?: FormItemRule => boolean, options?: AsyncValidatorOptions }) => Promise<{ warnings: FormValidationError | undefined }> 폼 아이템을 검증해요. 반환된 프로미스의 거부 값 타입은 FormValidationError예요. trigger를 설정하지 않으면 아이템의 모든 규칙이 적용돼요. shouldRuleBeApplied는 트리거로 필터된 규칙을 다시 걸러 줘요. warnings 2.37.1
restoreValidation () => void 검증 상태를 복원해요.
invalidateLabelWidth () => void 이 폼 아이템의 라벨 너비를 다시 계산해요. label-width="auto"를 쓰면서 라벨 내용이 동적으로 바뀔 때 유용해요. NEXT_VESION

AsyncValidatorOptions에 대해 더 알고 싶으면 async-validator를 참고하세요.

Form Slots

이름 파라미터 설명
default () 콘텐츠예요.

FormItem, FormItemGi Slots

이름 파라미터 설명 버전
default () 콘텐츠예요.
feedback () 피드백이에요. 2.24.0
label () 라벨 콘텐츠예요.

더 알아보기 (Learn more)

검증 규칙은 async-validator를 기반으로 동작하니, 이 문서의 표에 없는 세부 옵션은 async-validator 문서를 확인하세요. 라벨·피드백 배치를 그리드에 맞추고 싶다면 Grid와 GridItem 문서도 함께 보면 좋아요.