Skip to content

u-field component

This is a component for form fields, with each instance corresponding to one input in the form. We can use this component to implement two-way binding with custom input elements or input components, perform validation, and apply custom schemes.

This is a basic example

vue
<template>
  <u-field
    name="username"
    label="Username"
    help="Please input your username"
    validation="required"
    v-slot="{ value, update }"
  >
    <input :value="value" @input="($event) => update($event.target.value)" />
    <!-- or -->
    <input f-model />
  </u-field>
</template>

TIP

f-model is a syntax sugar for two-way binding similar to v-model. The difference is that it establishes two-way binding with its parent component, rather than with the component itself.

It will be transformed into the following HTML code

html
<div class="u-field-wrap">
  <div class="u-field-input-wrap">
    <label class="u-label">Username</label>
    <!-- input slot -->
  </div>
  <div class="u-field-help">Please input your username</div>
  <ul class="u-validation-message">
    <li>validation1 error</li>
    <!-- more validation error-->
  </ul>
</div>

It’s worth mentioning that when you install this library, it comes with no styles, which means you can freely add styles to match your own page design.

Not only that, you can also customize the structure between these HTML elements by using the custom prop.

This is an example of custom prop

vue
<template>
  <u-field v-slot="{ value, update, hasError, messages }" custom>
    <div class="my-input-wrap">
      <label>MyLabel</label>
      <input f-model />
      <div>My input help message</div>
      <ul class="validation-messages" v-if="hasError">
        <li v-for="msg in messages">{{ msg }}</li>
      </ul>
    </div>
  </u-field>
</template>

Other custom method

You can using scheme prop custom field's scheme:

vue
<script setup lang="ts">
import { h } from "vue";
import { SchemeArg } from "vue-uform";
const myScheme = (arg: SchemeArg) => {
  return h(
    "div",
    {
      style: { color: "green" },
    },
    [h("label", arg.label), arg.slot(), h("div", arg.help)]
  );
};
</script>
<template>
  <u-field
    :scheme="myScheme"
    name="username"
    label="Username"
    v-slot="{ value, update }"
  >
    <input f-model />
  </u-field>
  <u-field
    :scheme="myScheme"
    name="password"
    label="Password"
    v-slot="{ value, update }"
  >
    <input type="password" f-model />
  </u-field>
</template>

TIP

In addition to the custom and scheme mentioned above, we also support setting scheme on the u-form component to customize the structure of form fields. Their priorities are: custom prop > u-field's scheme > u-form's scheme > default configure.

Props

NameDescription
namebind with u-form component's values key
labelshow label message of the field,or show label in validation messages
helpform field input help message
validationbind validation rules with it's name
validation-messagescustom validation message using an object
rulesadd custom validation rules
customRe-layout form fields using HTML elements.
schemeUse Vue’s h function to assemble HTML elements, allowing the HTML structure to be reused between form fields.

Slot Argments

NameDescription
valuePass the value of u-field to its child components.
updateWhen the child component updates the data, this update event should be triggered.
hasErrorIf the validation fails, it will return true; the default is false.
messagesThis is a collection of all the validation error messages for this u-field.this is an array

Scheme Argments

NameDescription
namethe form field's key
labelthe field's showing label message
valueRefthe field's value,this is reactive
helpthe field's help message
hasErrorIf the validation fails, it will return true; the default is false.
messagesThis is a collection of all the validation error messages for this u-field. this is an array
validation_namesthe field added validation rules's name, tip: it can be used to display "*"