Skip to content

Form

The u-form component is used for form layout and data collection, and the u-form-item component is used to wrap form controls, providing label display and error message prompts.

Basic Usage

Basic form layout usage.

vue
<template>
  <u-form>
    <u-form-item label="Name" required>
      <u-input placeholder="Enter name" />
    </u-form-item>
    <u-form-item label="Email" required>
      <u-input placeholder="Enter email" />
    </u-form-item>
    <u-form-item>
      <u-button color="primary">Submit</u-button>
      <u-button>Reset</u-button>
    </u-form-item>
  </u-form>
</template>

Inline Layout

When inline is set to true, form items will be displayed horizontally.

vue
<template>
  <u-form inline>
    <u-form-item label="User">
      <u-input placeholder="User" />
    </u-form-item>
    <u-form-item label="Password">
      <u-input type="password" placeholder="Password" />
    </u-form-item>
    <u-form-item>
      <u-button color="primary">Login</u-button>
    </u-form-item>
  </u-form>
</template>

Label Position

Use the label-position property to control the position of the label, supporting left, right, and top.

vue
<template>
  <u-form label-position="top">
    <u-form-item label="Title">
      <u-input placeholder="Enter title" />
    </u-form-item>
    <u-form-item label="Description">
      <u-input type="textarea" placeholder="Enter description" />
    </u-form-item>
  </u-form>
</template>

Error Message

Display error messages through the message property.

vue
<template>
  <u-form>
    <u-form-item label="Name" required :message="errorMsg">
      <u-input v-model="name" @blur="validateName" />
    </u-form-item>
    <u-form-item>
      <u-button color="primary" @click="validateName">Validate</u-button>
    </u-form-item>
  </u-form>
</template>

<script setup>
import { ref } from 'vue';

const name = ref('');
const errorMsg = ref('');

const validateName = () => {
  if (!name.value) {
    errorMsg.value = 'Please enter name';
  } else if (name.value.length < 2) {
    errorMsg.value = 'Name must be at least 2 characters';
  } else {
    errorMsg.value = '';
  }
};
</script>

API

Form Props

PropertyDescriptionTypeAccepted ValuesDefault
labelPositionLabel positionstringleft / right / topright
labelWidthLabel widthstring--
inlineInline layoutboolean-false
disabledDisable the entire formboolean-false

FormItem Props

PropertyDescriptionTypeAccepted ValuesDefault
labelLabel textstring--
labelWidthLabel width (overrides form setting)string-80px
requiredWhether requiredboolean-false
messageError messagestring--
colorError message colorstringBuilt-in colorserror

FormItem Slots

Slot NameDescription
defaultForm control content
labelCustom label content