Form Form
Coordinate field state, validation, reset and error navigation.
Import
Use <hui-form> 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.
<template>
<view>
<hui-form
ref="control"
:model="model"
:rules="rules"
@validate="event0"
@scroll-to-field="event1"
><hui-form-item prop="name" label="姓名 / Name"
><hui-input v-model="model.name" clearable /></hui-form-item
><hui-form-item prop="note" label="备注 / Note"
><hui-textarea v-model="model.note" /></hui-form-item></hui-form
><hui-button @click="validate">校验 / Validate</hui-button
><hui-button @click="control?.resetFields()">重置 / Reset</hui-button>
<hui-button @click="() => control?.validate()">validate()</hui-button>
<hui-button @click="() => control?.validateField('name')">validateField()</hui-button>
<hui-button @click="() => control?.resetFields()">resetFields()</hui-button>
<hui-button @click="() => control?.clearValidate()">clearValidate()</hui-button>
<hui-button @click="() => control?.scrollToField('name')">scrollToField()</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, note: '' as string | number })
const rules: Record<string, FormRule[]> = {
name: [{ required: true, message: '请输入姓名 / Enter a name', trigger: 'blur' }]
}
const validate = async () => {
lastEvent.value = JSON.stringify(await control.value?.validate())
}
const event0 = (arg0: unknown, arg1: unknown, arg2: unknown) => {
lastEvent.value =
'validate' + ': ' + [describe(arg0), describe(arg1), describe(arg2)].join(', ')
}
const event1 = (position: { prop: string; top: number }) => {
lastEvent.value = 'scroll-to-field: ' + describe(position)
}
</script>
Validation returns a result rather than throwing for invalid fields. Register pending submit work as a blocker so unfinished uploads can prevent submission.
After validation fails, all invalid fields are measured and the field with the smallest viewport top is chosen, including fields inserted later. Both scrollToField and automatic error navigation emit scroll-to-field; top is the field’s viewport position in px.
For a form inside scroll-view, set :scroll-to-page="false" and handle the event by updating the container’s scroll-top: nextScrollTop = Math.max(0, currentScrollTop + top - containerTop). Measure containerTop with boundingClientRect in the same viewport coordinate system, and track the current scrollTop from the container’s scroll event.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
model | Record<string, unknown> | — | Form model object. |
rules | Record<string, FormRule<FormAbortSignal>[]> | — | Field validation rules. |
labelPosition | "left" | "right" | "top" | 'left' | Place field labels on the left, right-aligned or above. |
labelWidth | string | number | '6em' | Label width; numeric values use px. |
labelSuffix | string | '' | Text appended to field labels. |
hideRequiredAsterisk | boolean | false | Hide required markers without disabling validation. |
showMessage | boolean | true | Display field validation messages. |
size | ComponentSize | — | Component size; inherits configuration when omitted. |
disabled | boolean | false | Disable interaction. |
readonly | boolean | false | Read-only presentation without value changes. |
scrollToError | boolean | true | Scroll to the first invalid field after form validation. |
scrollToPage | boolean | true | Scroll the page after locating a field. Set to false to emit the position for a scrolling container instead. |
border | boolean | true | Show a border or separator. |
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 |
|---|---|---|
validate | (prop: string, valid: boolean, error?: string) | Field validation finished. |
scroll-to-field | (position: { prop: string; top: number }) | Field position; top is relative to the viewport in px. |
Slots
| Slot | Parameters | Description |
|---|---|---|
default | — | Main content. |
Methods
| Method | Signature | Description |
|---|---|---|
validate | (): Promise<FormValidateResult> | Validate and return the result. |
validateField | (props: string | string[]): Promise<FormValidateResult> | Validate only the specified fields. |
resetFields | (props?: string | string[] | undefined): void | Restore registered initial field values and clear validation. |
clearValidate | (props?: string | string[] | undefined): void | Clear validation presentation. |
scrollToField | (prop: string): Promise<void> | Scroll to the specified field. |
Types
FormProps、FormRule、FormValidateResult、FormValidateTrigger、FormExpose、FormEmits are available from @nscode/handy-uni.
export type FormRule = SharedFormRule
export type FormValidateResult = SharedFormValidateResult
export type FormValidateTrigger = SharedFormValidateTrigger
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.