Skip to content

Form ​

Data collection and validation. The validation logic is entirely pure functions of @lh-ui/share, with zero platform dependencies, so behavior is identical on both targets.

Basic usage ​

vue
<template>
  <lh-form :model="form" :rules="rules" ref="formRef" label-width="80">
    <lh-form-item label="Username" prop="name">
      <lh-input v-model="form.name" placeholder="Enter your username" />
    </lh-form-item>
    <lh-form-item label="Email" prop="email">
      <lh-input v-model="form.email" placeholder="Enter your email" />
    </lh-form-item>
    <lh-button type="primary" @click="onSubmit">Submit</lh-button>
  </lh-form>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const form = ref({ name: '', email: '' })
const formRef = ref()

const rules = {
  name: [{ required: true, message: 'Please enter your username', trigger: 'blur' }],
  email: [{ required: true, message: 'Please enter your email', trigger: 'blur' }],
}

const onSubmit = async () => {
  const ok = await formRef.value.validate()
  if (ok) {
    // validation passed, submit
  }
}
</script>

Form Props ​

PropDescriptionTypeDefault
modelForm data object (required)Record<string, unknown>—
rulesValidation rules (keys are prop names)Record<string, LhFormRule[]>—
labelPositionLabel position'left' | 'right' | 'top''right'
labelWidthLabel widthstring | number—
inlineInline layoutbooleanfalse
customClassCustom class namestring—
sxQuick layout propsLhSxProps—

Form Methods (called via ref) ​

ref calls must happen after onReady

At the onLoad stage the child components are not fully created yet, so formRef.value is undefined. The click event of the submit button naturally happens after onReady, so using it directly is fine; if you need to call it during page initialization, put it inside onReady.

MethodDescriptionReturns
validateValidate all fieldsPromise<boolean> (true when all pass)
validateFieldValidate a single fieldPromise<boolean>

FormItem Props ​

PropDescriptionTypeDefault
propField key (corresponding to the key of model and of rules)string—
labelLabel textstring—
requiredRequired marker (shows an asterisk; the actual validation is decided by rules)booleanfalse
rulesRules specific to this field (merged with the Form's rules)LhFormRule[]>—
customClassCustom class namestring—

Differences from the Vue3 version

  • Validation triggers: the Mini Program controls have no change semantics, so each form control maps its blur / input events to validation triggers.
  • Form submission: form semantics differ on the Mini Program, so validate returns a Promise<boolean> for manual submission; there is no submit event interception like on the web.