FormItem Form item
Connect a form model to controls, labels, hints and validation messages.
Import
Use <hui-form-item> with uni-app easycom. Import public types from @nscode/handy-uni. For ordinary Vue 3, install the preview plugin from @nscode/handy-uni/web and register its components.
Examples
Interact with the controls to see values and event results on the page.
vue
<template>
<view>
<hui-form :model="model"
><hui-form-item
ref="control"
prop="name"
label="姓名 / Name"
:rules="rules"
tip="至少两字 / At least two characters"
@click="event0"
><hui-input v-model="model.name" clearable /></hui-form-item></hui-form
><hui-button @click="validate">校验字段 / Validate field</hui-button
><hui-button @click="control?.clearValidate()">清除结果 / Clear validation</hui-button>
<hui-button @click="() => control?.validate()">validate()</hui-button>
<hui-button @click="() => control?.resetField()">resetField()</hui-button>
<hui-button @click="() => control?.clearValidate()">clearValidate()</hui-button>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { type FormRule } from '@nscode/handy-uni'
const lastEvent = ref('No event yet')
const describe = (value: unknown) => {
try {
return JSON.stringify(value) ?? String(value)
} catch {
return String(value)
}
}
const control = ref()
const model = ref({ name: '' as string | number })
const rules: FormRule[] = [
{ required: true, message: '请输入姓名 / Enter a name' },
{ min: 2, message: '至少两字 / At least two characters' }
]
const validate = async () => {
lastEvent.value = String((await control.value?.validate()) ?? '有效 / Valid')
}
const event0 = (arg0: unknown) => {
lastEvent.value = 'click' + ': ' + [describe(arg0)].join(', ')
}
</script>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
prop | string | — | Field path in the form model. |
label | string | — | Visible label. |
required | boolean | undefined | Required indication; derived from rules when omitted. |
rules | FormRule<FormAbortSignal>[] | — | Field validation rules. |
labelWidth | string | number | — | Label width; numeric values use px. |
labelPosition | "left" | "right" | "top" | — | Place field labels on the left, right-aligned or above. |
size | ComponentSize | — | Component size; inherits configuration when omitted. |
showMessage | boolean | undefined | Display field validation messages. |
readonly | boolean | undefined | Read-only presentation without value changes. |
error | string | — | Explicit error message overriding internal validation. |
tip | string | — | Field help text. |
isLink | boolean | false | Show a navigation arrow. |
Root attributes: common props. A Boolean default of undefined supports inheritance; explicit false overrides it. Other Boolean props default to false unless listed otherwise.
Events
| Event | Parameters | Description |
|---|---|---|
click | (event: unknown) | Emitted when the interactive area is clicked. |
Slots
| Slot | Parameters | Description |
|---|---|---|
label | — | Custom label region; replaces its fallback content. |
default | — | Main content. |
extra | — | Custom extra region; replaces its fallback content. |
error | { error } | Custom error region; replaces its fallback content. |
tip | — | Custom tip region; replaces its fallback content. |
Methods
| Method | Signature | Description |
|---|---|---|
validate | (trigger?: FormValidateTrigger | undefined): Promise<string | undefined> | Validate and return the result. |
resetField | (): void | Restore this field’s initial value. |
clearValidate | (): void | Clear validation presentation. |
Types
FormItemProps、FormItemExpose、FormItemEmits are available from @nscode/handy-uni.
Platform notes
| Behavior | uni-app H5 | WeChat Mini Program | App-vue | Ordinary Vue 3 Web (preview) |
|---|---|---|---|---|
| Basic interaction | Supported; device checks pending | Supported; device checks pending | Supported; device checks pending | Supported by the web platform adapter |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.