Skip to content

Form 表单

表单包含 输入框, 单选框, 下拉选择, 多选框 等用户输入的组件。 使用表单,您可以收集、验证和提交数据。

基础用法

最基础的表单包括各种输入表单项,比如input、select、switch等。

在每一个 form 组件中,你需要一个 form-item 字段作为输入项的容器,用于获取值与验证值。

form value:
{
  "email": "",
  "password": "",
  "confirmPwd": "",
  "agreement": false,
  "gender": ""
}

<script setup>
import { reactive, ref } from 'vue'
import PfForm from '@/components/lib/Form/src/Form.vue'
import PfFormItem from '@/components/lib/Form/src/FormItem.vue'
import PfInput from '@/components/lib/Input/src/Input.vue'
import PfButton from '@/components/lib/Button/src/Button.vue'
import PfSwitch from '@/components/lib/Switch/src/Switch.vue'
import PfSelect from '@/components/lib/Select/src/Select.vue'
const model = reactive({
  email: '',
  password: '',
  confirmPwd: '',
  agreement: false,
  gender: ''
})
const gender = [
  { label: '男', value: 'male' },
  { label: '女', value: 'female' },
  { label: '保密', value: 'secret' }
]
const formRef = ref()
const submit = async () => {
  alert('submitted!')
}
const reset = () => {
  formRef.value.resetFields()
}
</script>
<template>
  <div>
    <pf-form :model="model" ref="formRef">
      <pf-formItem prop="email" label="the email">
        <pf-input v-model="model.email" />
      </pf-formItem>
      <pf-formItem prop="password" label="the password">
        <pf-input v-model="model.password" type="password" />
      </pf-formItem>
      <pf-formItem prop="agreement" label="agreement">
        <pf-switch v-model="model.agreement" />
      </pf-formItem>
      <pf-formItem prop="gender" label="gender">
        <pf-select v-model="model.gender" :options="gender" />
      </pf-formItem>
      <pf-formItem>
        <pf-button @click.prevent="submit" type="primary">Submit</pf-button>
        <pf-button @click.prevent="reset">Reset</pf-button>
      </pf-formItem>
    </pf-form>

    <h4>
      form value:
      <pre>{{ model }}</pre>
    </h4>
  </div>
</template>

表单校验

Form 组件允许你验证用户的输入是否符合规范,来帮助你找到和纠正错误。

Form 组件提供了表单验证的功能,只需为 rules 属性传入约定的验证规则,并将 form-Item 的 prop 属性设置为需要验证的特殊键值即可。 更多高级用法可参考 async-validator。

form value:
{
  "email": "",
  "password": "",
  "confirmPwd": "",
  "agreement": false,
  "gender": ""
}

<script setup>
import { reactive, ref } from 'vue'
import PfForm from '@/components/lib/Form/src/Form.vue'
import PfFormItem from '@/components/lib/Form/src/FormItem.vue'
import PfInput from '@/components/lib/Input/src/Input.vue'
import PfButton from '@/components/lib/Button/src/Button.vue'
import PfSwitch from '@/components/lib/Switch/src/Switch.vue'
import PfSelect from '@/components/lib/Select/src/Select.vue'
const model = reactive({
  email: '',
  password: '',
  confirmPwd: '',
  agreement: false,
  gender: ''
})
const gender = [
  { label: '男', value: 'male' },
  { label: '女', value: 'female' },
  { label: '保密', value: 'secret' }
]
const rules = {
  email: [{ type: 'email', required: true, trigger: 'blur' }],
  password: [
    { type: 'string', required: true, trigger: 'blur', min: 3, max: 5 }
  ],
  confirmPwd: [{ type: 'string', required: true, trigger: 'blur' }],
  agreement: [
    { type: 'enum', required: true, enum: [true], message: '请同意协议' }
  ],
  gender: [{ type: 'string', required: true, trigger: 'change' }]
}
const formRef = ref()
const submit = async () => {
  try {
    await formRef.value.validate()
    console.log('passed!')
  } catch (e) {
    console.log('the promise', e)
  }
}
const reset = () => {
  formRef.value.resetFields()
}
</script>
<template>
  <div>
    <pf-form :model="model" :rules="rules" ref="formRef">
      <pf-formItem prop="email" label="the email">
        <pf-input v-model="model.email" />
      </pf-formItem>
      <pf-formItem prop="password" label="the password">
        <pf-input v-model="model.password" type="password" />
      </pf-formItem>
      <pf-formItem prop="agreement" label="agreement">
        <pf-switch v-model="model.agreement" />
      </pf-formItem>
      <pf-formItem prop="gender" label="gender">
        <pf-select v-model="model.gender" :options="gender" />
      </pf-formItem>
      <pf-formItem>
        <pf-button @click.prevent="submit" type="primary">Submit</pf-button>
        <pf-button @click.prevent="reset">Reset</pf-button>
      </pf-formItem>
    </pf-form>

    <h4>
      form value:
      <pre>{{ model }}</pre>
    </h4>
  </div>
</template>

自定义校验规则

展示了如何使用自定义验证规则来完成密码的二次验证。

form value:
{
  "email": "",
  "password": "",
  "confirmPwd": "",
  "agreement": false,
  "gender": ""
}

<script setup>
import { reactive, ref } from 'vue'
import PfForm from '@/components/lib/Form/src/Form.vue'
import PfFormItem from '@/components/lib/Form/src/FormItem.vue'
import PfInput from '@/components/lib/Input/src/Input.vue'
import PfButton from '@/components/lib/Button/src/Button.vue'
import PfSwitch from '@/components/lib/Switch/src/Switch.vue'
import PfSelect from '@/components/lib/Select/src/Select.vue'
const model = reactive({
  email: '',
  password: '',
  confirmPwd: '',
  agreement: false,
  gender: ''
})
const gender = [
  { label: '男', value: 'male' },
  { label: '女', value: 'female' },
  { label: '保密', value: 'secret' }
]
const rules = {
  email: [{ type: 'email', required: true, trigger: 'blur' }],
  password: [
    { type: 'string', required: true, trigger: 'blur', min: 3, max: 5 }
  ],
  confirmPwd: [
    { type: 'string', required: true, trigger: 'blur' },
    {
      validator: (rule, value) => value === model.password,
      message: '两次输入密码不一致',
      trigger: 'blur'
    }
  ],
  agreement: [
    { type: 'enum', required: true, enum: [true], message: '请同意协议' }
  ],
  gender: [{ type: 'string', required: true, trigger: 'change' }]
}
const formRef = ref()
const submit = async () => {
  try {
    await formRef.value.validate()
    console.log('passed')
  } catch (error) {
    console.log(error)
  }
}
const reset = () => {
  formRef.value.resetFields()
}
</script>
<template>
  <div>
    <pf-form :model="model" :rules="rules" ref="formRef">
      <pf-formItem prop="email" label="the email">
        <pf-input v-model="model.email" />
      </pf-formItem>
      <pf-formItem prop="password" label="the password">
        <pf-input v-model="model.password" type="password" />
      </pf-formItem>
      <pf-formItem prop="confirmPwd" label="confirm password">
        <pf-input v-model="model.confirmPwd" type="password" />
      </pf-formItem>
      <pf-formItem prop="agreement" label="agreement">
        <pf-switch v-model="model.agreement" />
      </pf-formItem>
      <pf-formItem prop="gender" label="gender">
        <pf-select v-model="model.gender" :options="gender" />
      </pf-formItem>
      <pf-formItem>
        <pf-button @click.prevent="submit" type="primary">Submit</pf-button>
        <pf-button @click.prevent="reset">Reset</pf-button>
      </pf-formItem>
    </pf-form>

    <h4>
      form value:
      <pre>{{ model }}</pre>
    </h4>
  </div>
</template>

Form 属性

属性名说明类型默认值
model表单数据对象object
rules表单验证规则object
label-suffix表单域标签的后缀string''
labelWidth标签的宽度number80
labelPosition标签的位置 left | rightstring'right'

Form 事件

名称说明类型
validate任一表单项被校验后触发Function

Form 公共方法

名称说明类型
validate对整个表单的内容进行验证。 接收一个回调函数,或返回 Promise。Function
resetFields重置该表单项,将其值重置为初始值,并移除校验结果Function
clearValidate清理某个字段的表单验证信息。Function

FormItem 属性

属性名说明类型Default
propmodel 的键名。 它可以是一个属性的值(如 a.b.0 或 ['a', 'b', '0'])。 在使用了 validate、resetFields 的方法时,该属性是必填的。string / string[]
label标签文本string
labelWidth标签的宽度number

FormItem 事件

名称说明类型
validate任一表单项被校验后触发Function

FormItem 插槽

插槽名说明类型
default表单的内容。
label标签位置显示的内容object

FormItem 公共方法

名称说明类型
validate验证表单项Function
resetField对该表单项进行重置,将其值重置为初始值并移除校验结果Function
clearValidate移除该表单项的校验结果Function