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

FormField

원문 보기 위키 갱신

FormField (폼 필드)

FormField는 폼 요소를 감싸서 검증(validation)과 오류 처리(error handling)를 제공하는 래퍼 컴포넌트예요. 라벨, 설명, 도움말, 오류 메시지를 한곳에서 깔끔하게 관리할 수 있어요.

출처: Nuxt UI

본문

어떤 폼 컴포넌트든 FormField로 감싸서 사용할 수 있어요. Form 안에서 사용하면 검증과 오류 처리를 제공해 줘요.

라벨 (Label)

label prop을 사용해 폼 컨트롤의 라벨을 설정할 수 있어요.

<template>
  <UFormField label="Email">
    <UInput placeholder="Enter your email" />
  </UFormField>
</template>

[!NOTE]

id가 제공되지 않으면 라벨의 for 속성과 폼 컨트롤이 고유한 id로 연결돼요.

required prop을 사용하면 라벨 옆에 별표가 추가돼요.

<template>
  <UFormField label="Email" required>
    <UInput placeholder="Enter your email" />
  </UFormField>
</template>

설명 (Description)

description prop을 사용해 라벨 아래에 추가 정보를 제공할 수 있어요.

<template>
  <UFormField label="Email" description="We'll never share your email with anyone else.">
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>

힌트 (Hint)

hint prop을 사용해 라벨 옆에 힌트 메시지를 표시할 수 있어요.

<template>
  <UFormField label="Email" hint="Optional">
    <UInput placeholder="Enter your email" />
  </UFormField>
</template>

도움말 (Help)

help prop을 사용해 폼 컨트롤 아래에 도움말 메시지를 표시할 수 있어요. error prop과 함께 사용하면 error prop이 우선해요.

<template>
  <UFormField label="Email" help="Please enter a valid email address.">
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>

오류 (Error)

error prop을 사용해 폼 컨트롤 아래에 오류 메시지를 표시할 수 있어요. help prop과 함께 사용하면 error prop이 우선해요.

Form 안에서 사용하면 검증 오류가 발생했을 때 이 값이 자동으로 설정돼요.

<template>
  <UFormField label="Email" error="Please enter a valid email address.">
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>

[!TIP] 참고: /docs/getting-started/theme/design-system#color-system

이 prop은 폼 컨트롤의 color를 error로 설정해요. app.config.ts에서 전역적으로 변경할 수 있어요.

오류 패턴 (Error Pattern)

error-pattern prop을 사용해 정규식을 통해 폼 오류를 매칭할 수 있어요. 이는 InputTags처럼 배열 값을 가진 컴포넌트에서 특히 유용한데, 이런 경우 오류 이름에 배열 인덱스(예: tags.0)가 포함되기 때문이에요.

[!TIP] 참고: /docs/components/form#error-reporting

Form 안에서 error-pattern을 사용하는 예제를 확인해 보세요.

크기 (Size)

size prop을 사용해 FormField의 크기를 변경할 수 있어요. size는 폼 컨트롤로 전달(proxy)돼요.

<template>
  <UFormField label="Email" description="We'll never share your email with anyone else." hint="Optional" help="Please enter a valid email address." size="xl">
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>

방향 (Orientation) 4.3+

orientation prop을 사용해 FormField의 레이아웃을 변경할 수 있어요. 기본값은 vertical이에요.

<template>
  <UFormField orientation="horizontal" label="Email" help="Please enter a valid email address." class="w-72">
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>

API

Props

/**
 * Props for the FormField component
 */
interface FormFieldProps {
  /**
   * The element or component this component should render as.
   * @default 'div'
   */
  as?: any;
  /**
   * The name of the FormField. Also used to match form errors.
   */
  name?: string | undefined;
  /**
   * A regular expression to match form error names. Useful for components with array values such as InputTags, where errors include array indices in their name (e.g. `tags.0`).
   */
  errorPattern?: RegExp | undefined;
  label?: string | undefined;
  description?: string | undefined;
  help?: string | undefined;
  /**
   * @default undefined
   */
  error?: string | false | true | undefined;
  hint?: string | undefined;
  /**
   * @default 'md'
   */
  size?: "md" | "xs" | "sm" | "lg" | "xl" | undefined;
  required?: boolean | undefined;
  /**
   * If true, validation on input will be active immediately instead of waiting for a blur event.
   */
  eagerValidation?: boolean | undefined;
  /**
   * Delay in milliseconds before validating the form on input events.
   * @default `300`
   */
  validateOnInputDelay?: number | undefined;
  /**
   * The orientation of the form field.
   * @default 'vertical'
   */
  orientation?: "vertical" | "horizontal" | undefined;
  ui?: { root?: SlotClass; wrapper?: SlotClass; labelWrapper?: SlotClass; label?: SlotClass; container?: SlotClass; description?: SlotClass; error?: SlotClass; hint?: SlotClass; help?: SlotClass; } | undefined;
}

Slots

/**
 * Slots for the FormField component
 */
interface FormFieldSlots {
  label(): any;
  hint(): any;
  description(): any;
  help(): any;
  error(): any;
  default(): any;
}

테마 (Theme)

export default defineAppConfig({
  ui: {
    formField: {
      slots: {
        root: '',
        wrapper: '',
        labelWrapper: 'flex content-center items-center justify-between gap-1',
        label: 'block font-medium text-default',
        container: 'relative',
        description: 'text-muted',
        error: 'mt-2 text-error',
        hint: 'text-muted',
        help: 'mt-2 text-muted'
      },
      variants: {
        size: {
          xs: {
            root: 'text-xs'
          },
          sm: {
            root: 'text-xs'
          },
          md: {
            root: 'text-sm'
          },
          lg: {
            root: 'text-sm'
          },
          xl: {
            root: 'text-base'
          }
        },
        required: {
          true: {
            label: "after:content-['*'] after:ms-0.5 after:text-error"
          }
        },
        orientation: {
          vertical: {
            container: 'mt-1'
          },
          horizontal: {
            root: 'flex justify-between place-items-baseline gap-2'
          }
        }
      },
      defaultVariants: {
        size: 'md'
      }
    }
  }
})

변경 로그 (Changelog)

컴포넌트와 테마의 커밋 기록을 확인해 보세요.

더 알아보기 (Learn more)