更新记录
1.0.0(2025-02-24)
下载此版本
发布初版
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
app-vue |
× |
√ |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
鸿蒙元服务 |
× |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
√ |
Form 表单
组件名: xt-form
适配情况需自行尝试,开发时只测试了H5、APP、微信小程序
安装方式
本组件符合 easycom 规范,HBuilderX 3.1.0 起,只需将本组件导入项目,在页面 template 中即可直接使用,无需在页面中 import 和注册 components
基本用法
<template>
<view class="xt">
<xt-form ref="form" :model="form" :rules='rules'>
<xt-form-item label="姓名" prop="name" required>
<xt-input v-model="form.name" placeholder="请输入姓名"></xt-input>
</xt-form-item>
<xt-form-item label="性别" prop="sex">
<xt-radio v-model="form.sex">
<xt-radio-item label="男" value="1"></xt-radio-item>
<xt-radio-item label="女" value="0"></xt-radio-item>
</xt-radio>
</xt-form-item>
<xt-form-item label="简介" prop="brief">
<xt-textarea v-model="form.brief" placeholder="请输入简介" count></xt-textarea>
</xt-form-item>
</xt-form>
<xt-button @click="log">提交</xt-button>
<xt-button @click="reset">重置</xt-button>
<xt-button @click="clearValidate">清空校验</xt-button>
</view>
</template>
<script>
export default {
data() {
return {
form: {
name: '姓名',
sex: '1',
brief: '简介'
},
rules: {
name: {
type: 'string',
required: true,
max: 10,
message: '姓名不能超过10个字'
},
sex: {
type: 'string',
message: '请选择性别',
},
brief: {
pattern: /^[0-9a-zA-Z]*$/g,
transform(value) {
return String(value);
},
message: '只能包含字母或数字'
}
}
}
},
methods: {
// 提交
log() {
this.$refs.form.validate().then(res => {
console.log('通过')
})
},
// 重置
reset() {
this.$refs.form.resetFields()
},
// 清空校验
clearValidate() {
this.$refs.form.clearValidate()
}
}
}
</script>
<style lang="scss">
.xt {
padding: 0 20rpx;
padding-top: 50rpx;
display: flex;
flex-direction: column;
gap: 15rpx;
}
</style>
xt-form API
属性 |
类型 |
说明 |
默认值 |
model |
Object |
需要验证字段的集合 |
无 |
labelWidth |
String | Number |
提示文字的宽度 |
65rpx |
labelAlign |
String |
label 字体的位置 |
left (可选 right``center``top ) |
labelStyle |
Object |
label 的样式 |
无 |
borderBottom |
Boolean |
是否显示表单项的下划线边框 |
false |
gap |
String |
表单项之间的间距 |
20rpx |
rules |
Object |
校验规则 |
无 |
disabled |
Boolean |
是否禁用整个表单 |
false |
xt-form-item API
属性 |
类型 |
说明 |
默认值 |
label |
String |
表单项标题 |
无 |
prop |
String |
表单项字段 |
无 |
labelWidth |
String | Number |
表单项标题宽度,优先级大于父组件 |
无 |
required |
Boolean |
是否显示 * 号 |
false |
disabled |
Boolean |
是否禁用表单项 |
false |
Events(通过ref调用)
事件名 |
描述 |
参数 |
setRules |
手动设置校验规则 |
Function(rules) |
validate |
对整个表单进行校验 |
|
validateField |
对某个表单项进行校验 |
Function(value,Function(err)) |
clearValidate |
清空校验结果 |
|
resetFields |
重置表单 |
|
Rules校验规则
组件使用了 async-validator 验证字段,这里对常用的进行讲解,更多请查看官网的文档说明
trigger
{String | Array}: 触发校验方式
- change: 字段发生变化时触发
- blur: 子组件失去焦点触发
- 需要搭配
xtUI
才有效
type
{String}: 内置的校验规则
- string:必须是
string
类型,默认类型
- number:必须是
number
类型
- boolean:必须是
boolean
类型
- method:必须是
function
类型
- regexp:必须是
regexp
类型,这里的正则,指的是判断字段的内容是否一个正则表达式,而不是用这个正则去匹配字段值
- integer:必须是
整数
类型
- float:必须是
浮点数
类型
- array:必须是
array
类型
- object:必须是
object
类型
- enum:必须出现在
enmu
指定的值中
- date:必须是
date
类型
- url:必须是
url
类型
- hex:必须是
16
进制类型
- email:必须是
email
类型
- any:任意类型
required
{Boolean}: 是否必填
pattern
: 正则表达式,如 /^[0-9a-zA-Z]*$/g 只能包含数字字母
min
: 最小值,如果字段类型为字符串和数组,会取字符串长度与数组长度(length)与min
比较,如果字段是数值,则直接与min
比较。
max
: 最大值,与min
同理
len
:指定长度,规则同min
,优先级高于min
和max
enum
{Array} 指定的值,配合 type: 'enum' 使用
whitespace
{Boolean}
如果字段值内容都为空格,默认无法通过required: true
校验,如果要允许通过,需要将此参数设置为true
transform
{Function},校验前对值进行转换,函数的参数为当前值,返回值为改变后的值,参数如如下:
message
校验不通过时的提示信息
validator
{Function}:自定义同步校验函数,参数如下:
rule
:当前校验字段在 rules 中所对应的校验规则
value
:当前校验字段的值
callback
:校验完成时的回调,一般无需执行callback,返回true
(校验通过)或者false
(校验失败)即可
asyncValidator
{Function}:自定义异步校验函数,参数如下:
rule
:当前校验字段在 rules 中所对应的校验规则
value
:当前校验字段的值
callback
:校验完成时的回调,执行完异步操作(比如向后端请求数据验证),如果不通过,需要callback(new Error('提示错误信息')),如果校验通过,执行callback()即可