表单 xForm
预览
介绍
从1.1.2开始允许xform可以嵌套view进行form-item布局了,但建议不要嵌套太深,影响性能. 嵌套对象字段支持 / 和 . 两种写法,如 user/id、user.id,两者等价,rules 的 key 同样适用。
平台兼容
| Harmony | H5 | andriod | IOS | 小程序 | UTS | UNIAPP-X SDK | version |
|---|---|---|---|---|---|---|---|
| ☑ | ☑ | ☑️ | ☑️ | ☑️ | ☑️ | 4.76+ | 1.1.18 |
文件路径
ts
@/uni_modules/tmx-ui/components/x-form/x-form.uvue使用
ts
<x-form></x-form>Props 属性
| 名称 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| labelWidth | 标签宽(横向表单时有效)不支持动态修改(uniappx 3.99不支持动态传递 inject),可在子组件上动态修改 | string | "100" |
| labelDirection | 不支持动态修改(uniappx 3.99不支持动态传递 inject),可在子组件上动态修改vertical,horizontal | labelDirType | "horizontal" |
| labelFontColor | 标签的文本颜色,不支持动态修改(uniappx 3.99不支持动态传递 inject),可在子组件上动态修改 | string | "#333333" |
| errorAutoPage | 出错时,是否滚动到表单的位置不支持动态修改(uniappx 3.99不支持动态传递 inject),可在子组件上动态修改 | boolean | true |
| modelValue | 等同v-model | any | {} as Record<string, any> |
| labelFontSize | 标签文本大小不支持动态修改(uniappx 3.99不支持动态传递 inject),可在子组件上动态修改 | string | "16" |
| showLabel | 是否显示标题,不支持动态修改(uniappx 3.99不支持动态传递 inject),可在子组件上动态修改 | boolean | true |
| errorAlign | 错误标签的对齐方式,left,center,right | errorAlignType | 'left' |
| rules | 字段规则集合,推荐对象:{ username: [{ type: 'string' }] }。与 formItem 上的 rule 不冲突,两边都有时同名字段会校验两次,只提供一边则校验一次。仍兼容旧的 Map<string, FORM_RULE[]>。 | union | ():FORM_RULES => ({} as FORM_RULES) |
| watchValidStatus | 是否开启实时全部字段校验获取当前实时的校验状态,通过vmodel:valid对外输出当前实时的校验值,当字段值改变时会一直监测并监听所有字段并返回结果到对外如果你没有这个需求场景,请保持关闭状态,如果你确实外部需要实时改变并观察提交按钮状态可以打开,请在字段少的场景使用。 | boolean | false |
| modelValid | 当前的校验状态,请不要外部改变此值,此值只对外输出当前校验状态需要设置watchValidStatus为true才会实时监听并检测状态。请通过v-model:valid来得到状态值 | boolean | false |
Events 事件
| 名称 | 参数 | 说明 |
|---|---|---|
| submit | result: FORM_SUBMIT_RESULT | 按钮提交表单时触发。 |
| update:modelValid | - | 校验事件,这个不是submit是用户在输入内容或者在校验阶段向外发出的事件 里面包含了实时的校验信息,用于绑定外部的按钮提供实时的可提交状态。 它对外输出,不能够双向绑定对内更改校验状态。 |
Slots 插槽
| 名称 | 说明 | 数据 |
|---|---|---|
| default | 插槽内只能放置x-form-item子节点组件 | - |
Ref 方法
| 名称 | 参数 | 返回值 | 说明 |
|---|---|---|---|
| id | - | - | - |
| checkAsyncVaildStatus | - | - | 用来首次同步检测:modelValid,一定要在onready生命期中执行,并且你赋值完表数据后再来个,nextTick中执行本方法,可以确保不会有遗漏。 |
| valid | - | - | 手动校验。空数组校验全部已注册字段,model 里没有的键按空值处理。 options 不传时显示错误并触发 submit。静默校验传 showError / emitSubmit。 |
| clearValid | - | - | 清除校验状态并回到初始状态。 |
| setFieldError | - | - | 把服务端错误写到对应字段上,不重新跑本地规则。 |
| clearFieldError | - | - | 清除一个字段的错误。key 为空时清除全部。 |
| submit | - | - | - |
示例文件路径
json
/pages/biaodan/form示例源码
uvue
vue
<template>
<!-- #ifdef MP-WEIXIN || MP-ALIPAY -->
<page-meta :page-style="`background-color:${xThemeConfigBgColor}`">
<navigation-bar :background-color="xThemeConfigNavBgColor"
:front-color="xThemeConfigNavFontColor"></navigation-bar>
</page-meta>
<!-- #endif -->
<x-sheet>
<x-text font-size="18" class=" text-weight-b mb-8">表单 xForm</x-text>
<x-text color="#999999">与官方不同的是,它只为数据进行验证,因此不仅仅局限表单组件。而是任意数据进行的验证收集,比官方更灵活,与web组件对齐的校验。</x-text>
</x-sheet>
<x-sheet>
<x-form ref="form" :rules="rules" @submit="(submitData as resultTtype)"
v-model="(reqData as USER_TYPE)">
<x-form-item field="username" label="联系姓名" :required="true">
<x-input cursor-color="red" color='transparent' v-model="(reqData.username as string)"
align="right"></x-input>
</x-form-item>
<x-form-item field="testChildrenForm.text" label="嵌套字段" :required="true">
<x-input cursor-color="red" color='transparent' v-model="(reqData.testChildrenForm.text as string)"
align="right"></x-input>
</x-form-item>
<x-form-item v-if="reqData.testFiled!=null" field="testFiled" label="测试可空字段" :required="true">
<x-input cursor-color="red" color='transparent' v-model="(reqData.testFiled as string|null)"
align="right"></x-input>
</x-form-item>
<x-form-item field="title" label="产品标题" :required="true">
<x-input color='transparent' v-model="(reqData.title as string)" align="right"></x-input>
</x-form-item>
<x-form-item field="price" label="产品价格" :required="true">
<x-input color='transparent' type="number" v-model="(reqData.price as string)" right-text="万元"
align="right"></x-input>
</x-form-item>
<x-form-item :show-bottom-boder="false" field="num" label="库存数量" :required="true">
<view class="flex flex-row flex-row-center-end">
<x-stepper width="120" v-model="(reqData.num as number)"></x-stepper>
</view>
</x-form-item>
<x-form-item label="上传图片" field="upload" :required="true" contentStyle="display:flex;flex-direction:row;justify-content:flex-end">
<x-upload-media :max-count="3" align="right" v-model="(reqData.upload as XUPLOADFILE_FILE_VALUE[])" :column="3"
style="width: 200px;"></x-upload-media>
</x-form-item>
<x-form-item field="radio" label="选择性别" :required="true">
<x-radio-group v-model="(reqData.radio as string)" class="flex flex-row flex-row-center-end">
<x-radio class="mr-20" value="男" label="男"></x-radio>
<x-radio value="女" label="女"></x-radio>
</x-radio-group>
</x-form-item>
<x-form-item field="checkbox" label="选择水果" :required="true">
<x-checkbox-group v-model="(reqData.checkbox as string[])"
class="flex flex-row flex-row-center-end">
<x-checkbox v-for="(item,index) in ['苹果','香蕉','梨子','地龙']" :key="index" class="ml-20 mb-10"
:value="item" :label="item"></x-checkbox>
</x-checkbox-group>
</x-form-item>
<x-button form-type="form" class="mt-32" :block="true">保存资料</x-button>
<x-text font-size="12" color="error"
class="text-align-center pt-24">表单收集与组件不是绑定结构只与form-item绑定,因此不会像官方那样局限,可以作为一个数据验证器来理解。</x-text>
</x-form>
</x-sheet>
<x-sheet>
<x-text font-size="18" class=" text-weight-b mb-8">ref方法对上述表单验证</x-text>
<view class="flex flex-row flex-row-center-between">
<x-button style="width: calc(33.33% - 3px);" @click="submitByClick">ref验证</x-button>
<x-button style="width: calc(33.33% - 3px);" @click="clearValid">清除验证</x-button>
<x-button style="width: calc(33.33% - 3px);" @click="submitref">仅用户名</x-button>
</view>
</x-sheet>
<x-sheet>
<x-text font-size="18" class=" text-weight-b mb-8">风格可以自己随意变化</x-text>
<x-text color="#999999">下面开启watchValidStatus并绑定v-model:modelValid实时管理提交按钮校验状态。</x-text>
</x-sheet>
<x-sheet>
<x-form v-model:modelValid="(isPass as boolean)" :watchValidStatus="true" @submit="(submit as resultTtype)" v-model="(logindata as LOGIN_TYPE)"
:label-direction="('vertical' as labelDirType)">
<x-form-item :cell-padding="['0','0']" :showBottomBorder="false" :rule="rules.username" field="user"
label="用户帐号" :required="true">
<x-input darkBgColor="" left-icon="account-pin-circle-fill" placeholder="请输入你的帐号"
v-model="(logindata.user as string)"></x-input>
</x-form-item>
<x-form-item :cell-padding="['0','0']" :showBottomBorder="false" :rule="rules.password" field="pass"
label="登录密码" :required="true">
<x-input darkBgColor="" :password="true" left-icon="lock-password-fill" placeholder="请输入8位密码"
v-model="(logindata.pass as string)"></x-input>
</x-form-item>
<x-button form-type="form" :disabled="!isPass" class="mt-32" icon="lock-unlock-fill" :block="true">登入</x-button>
</x-form>
</x-sheet>
<x-sheet>
<x-text font-size="18" class=" text-weight-b mb-8">JSON 规则</x-text>
<x-text color="#999999">邮箱选填,空值通过,有值才校验格式。编码、城市、确认密码只写数据规则,不写函数。动态字段卸掉后不再参与校验。</x-text>
</x-sheet>
<x-sheet>
<x-form ref="dslForm" :rules="dslRules" v-model="(dslData as DSL_TYPE)" @submit="(onDslSubmit as resultTtype)">
<x-form-item field="email" label="邮箱(选填)" :required="false">
<x-input color="transparent" align="right" placeholder="可空" v-model="(dslData.email as string)"></x-input>
</x-form-item>
<x-form-item field="code" label="编码" :required="true">
<x-input color="transparent" align="right" placeholder="字母或数字" v-model="(dslData.code as string)"></x-input>
</x-form-item>
<x-form-item field="city" label="城市" :required="true">
<x-input color="transparent" align="right" placeholder="北京/上海/深圳" v-model="(dslData.city as string)"></x-input>
</x-form-item>
<x-form-item field="password" label="密码" :required="true">
<x-input color="transparent" :password="true" align="right" v-model="(dslData.password as string)"></x-input>
</x-form-item>
<x-form-item field="confirm" label="确认密码" :required="true">
<x-input color="transparent" :password="true" align="right" v-model="(dslData.confirm as string)"></x-input>
</x-form-item>
<x-form-item v-if="showExtra" field="extra" label="动态字段" :required="true">
<x-input color="transparent" align="right" v-model="(dslData.extra as string)"></x-input>
</x-form-item>
<view class="flex flex-row flex-row-center-between mt-32">
<x-button style="width: calc(50% - 6px);" @click="toggleExtra">动态字段</x-button>
<x-button style="width: calc(50% - 6px);" @click="silentValid">静默校验</x-button>
</view>
<view class="flex flex-row flex-row-center-between mt-12">
<x-button style="width: calc(50% - 6px);" @click="markServer">服务端错误</x-button>
<x-button style="width: calc(50% - 6px);" @click="clearServer">清除错误</x-button>
</view>
<x-button form-type="form" class="mt-12" :block="true">提交 JSON 规则</x-button>
</x-form>
</x-sheet>
<view style="height: 550px;"></view>
</template>
<script lang="ts" setup>
import { ref } from "vue"
import type { FORM_RULE, FORM_SUBMIT_RESULT, FORM_VALID_OPTIONS, XUPLOADFILE_FILE_VALUE } from "@/uni_modules/tmx-ui"
// @ts-ignore
const form = ref<XFormComponentPublicInstance | null>(null)
// @ts-ignore
const dslForm = ref<XFormComponentPublicInstance | null>(null)
type resultTtype = (evt : FORM_SUBMIT_RESULT) => void;
type labelDirType = 'vertical' | 'horizontal'
type USER_TYPE_CHILDRENFORM = {
text:string
}
type USER_TYPE = {
username : string;
password : string;
title : string;
price : string;
num : number;
testFiled ?: string,
radio : string,
checkbox : string[],
testChildrenForm:USER_TYPE_CHILDRENFORM,
upload : XUPLOADFILE_FILE_VALUE[]
};
type LOGIN_TYPE = {
user : string,
pass : string
}
const isPass = ref(false)
// RULES可以集中管理,也可以单位分开管理。到form-item rule上或者form中的rules上
type DEMO_FORM_RULES = {
username : FORM_RULE[],
"testChildrenForm.text" : FORM_RULE[],
password : FORM_RULE[],
title : FORM_RULE[],
price : FORM_RULE[],
num : FORM_RULE[],
upload : FORM_RULE[],
}
const rules : DEMO_FORM_RULES = {
username: [
{
type: "string",
errorMessage: "姓名填写不正确,不能空,且要小于4个字符",
trigger: 'blur',
valid: (val : any | null) : boolean => {
let pval = val as string;
return pval.length > 0 && pval.length <= 4
}
}
],
"testChildrenForm.text": [
{
type: "string",
errorMessage: "嵌套字段不能空,且要小于4个字符",
trigger: 'blur',
valid: (val : any | null) : boolean => {
let pval = val as string;
return pval.length > 0 && pval.length <= 4
}
}
],
password: [
{
type: "number",
errorMessage: "请输入8位密码",
valid: (val : any | null) : boolean => {
let pval = val as string;
return pval.length == 8
}
}
],
title: [
{
type: "string",
errorMessage: "产品标题不空,且大于5个字符小于12个字符",
valid: (val : any | null) : boolean => {
let pval = val as string;
return pval.length > 5 && pval.length <= 12
}
}
],
price: [
{
type: "number",
errorMessage: "价格不能小于30元",
min: 30
}
],
num: [
{
type: "number",
errorMessage: "商品库存在2-200之间",
min: 2,
max: 100
}
],
upload: [
{
type: "array",
errorMessage: "请最少选择1张图片最多3张",
min: 1,
max: 3
}
]
}
const reqData = ref<USER_TYPE>({
username: "",
password: "",
title: "",
price: "",
num: 0,
radio: "",
checkbox: [] as string[],
testChildrenForm:{
text:""
},
upload: [] as XUPLOADFILE_FILE_VALUE[]
})
const logindata = ref<LOGIN_TYPE>({
user: "",
pass: ""
})
const submitData = (evt : FORM_SUBMIT_RESULT) => {
console.log(evt)
if (!evt.valid) {
uni.showToast({ title: evt.errorMessage, icon: 'none' })
return;
}
}
const submit = (evt : FORM_SUBMIT_RESULT) => {
if (!evt.valid) {
uni.showToast({ title: evt.errorMessage, icon: 'none' })
return;
}
console.log(evt)
}
// 只验证username
const submitref = () => {
form.value!.valid(['username'] as string[])
}
//验证全部
const submitByClick = () => {
let result = form.value!.valid([] as string[]) as FORM_SUBMIT_RESULT
// console.log(result)
}
const clearValid = () => {
form.value!.clearValid()
}
const showExtra = ref(false)
type DSL_TYPE = {
email : string,
code : string,
city : string,
password : string,
confirm : string,
extra : string
}
type DSL_RULES = {
email : FORM_RULE[],
code : FORM_RULE[],
city : FORM_RULE[],
password : FORM_RULE[],
confirm : FORM_RULE[],
extra : FORM_RULE[]
}
const dslData = ref<DSL_TYPE>({
email: "",
code: "",
city: "",
password: "",
confirm: "",
extra: ""
})
const dslRules : DSL_RULES = {
email: [
{
type: "email",
errorMessage: "邮箱格式不正确"
}
],
code: [
{
required: true,
errorMessage: "请填写编码"
},
{
pattern: "^[A-Za-z0-9]+$",
errorMessage: "编码只能是字母或数字"
}
],
city: [
{
required: true,
oneOf: ["北京", "上海", "深圳"] as string[],
errorMessage: "城市只能是北京、上海、深圳"
}
],
password: [
{
type: "string",
min: 6,
max: 16,
errorMessage: "密码需要 6-16 位"
}
],
confirm: [
{
required: true,
errorMessage: "请再次输入密码"
},
{
validator: "equalTo",
validatorArgs: "password",
errorMessage: "两次密码不一致"
}
],
extra: [
{
type: "string",
min: 1,
errorMessage: "请填写动态字段"
}
]
}
const onDslSubmit = (evt : FORM_SUBMIT_RESULT) => {
uni.showToast({ title: evt.valid ? "通过" : evt.errorMessage, icon: "none" })
}
const toggleExtra = () => {
showExtra.value = !showExtra.value
}
const silentValid = () => {
let result = dslForm.value!.valid([] as string[], {
showError: true,
emitSubmit: false
} as FORM_VALID_OPTIONS) as FORM_SUBMIT_RESULT
uni.showToast({ title: result.valid ? "静默校验通过" : result.errorMessage, icon: "none" })
}
const markServer = () => {
dslForm.value!.setFieldError("code", "服务端:编码已存在")
}
const clearServer = () => {
dslForm.value!.clearFieldError("code")
}
</script>
<style lang="scss">
</style>