Skip to content

Commit 8e6096a

Browse files
feat: ✨ Form 表单 validate 方法支持传入数组 (#829)
Closes: #797
1 parent 8d528cb commit 8e6096a

5 files changed

Lines changed: 194 additions & 27 deletions

File tree

docs/component/form.md

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Form 表单 <el-tag text style="vertical-align: middle;margin-left:8px;" effect="plain">0.2.0</el-tag>
1+
# Form 表单
22

33
用于数据录入、校验,支持输入框、单选框、复选框、文件上传等类型,常见的 form 表单为`单元格`形式的展示,即左侧为表单的标题描述,右侧为表单的输入。
44

@@ -394,7 +394,7 @@ const submit = () => {
394394

395395
## 指定字段校验
396396

397-
`validate` 方法可以传入一个 `prop` 参数,指定校验的字段,可以实现在表单组件的`blur``change`等事件触发时对该字段的校验。
397+
`validate` 方法可以传入一个 `prop` 参数,指定校验的字段,可以实现在表单组件的`blur``change`等事件触发时对该字段的校验。`prop` 参数也可以是一个字段数组,指定多个字段进行校验。
398398

399399
::: details 查看指定字段校验示例
400400
::: code-group
@@ -424,6 +424,7 @@ const submit = () => {
424424
</wd-cell-group>
425425
<view class="footer">
426426
<wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
427+
<wd-button type="primary" size="large" @click="handleValidate" block>校验用户名和密码</wd-button>
427428
</view>
428429
</wd-form>
429430
```
@@ -459,6 +460,85 @@ function handleSubmit() {
459460
console.log(error, 'error')
460461
})
461462
}
463+
464+
function handleValidate() {
465+
form
466+
.value!.validate(['value1', 'value2'])
467+
.then(({ valid, errors }) => {
468+
if (valid) {
469+
showSuccess({
470+
msg: '校验通过'
471+
})
472+
}
473+
})
474+
.catch((error) => {
475+
console.log(error, 'error')
476+
})
477+
}
478+
</script>
479+
```
480+
481+
```css [css]
482+
.footer {
483+
padding: 12px;
484+
}
485+
```
486+
487+
:::
488+
489+
## 不对隐藏组件做校验
490+
491+
在表单中,如果某个组件使用 `v-if` 隐藏,则不会对该组件进行校验。
492+
493+
::: details 查看不对隐藏组件做校验示例
494+
::: code-group
495+
496+
```html [vue]
497+
<wd-form ref="form" :model="model" :rules="rules">
498+
<wd-cell-group border>
499+
<wd-input
500+
label="用户名"
501+
label-width="100px"
502+
prop="value1"
503+
clearable
504+
v-model="model.value1"
505+
placeholder="请输入用户名"
506+
:rules="[{ required: true, message: '请填写用户名' }]"
507+
/>
508+
<wd-input
509+
v-if="showPassword"
510+
label="密码"
511+
label-width="100px"
512+
prop="value2"
513+
show-password
514+
clearable
515+
v-model="model.value2"
516+
placeholder="请输入密码"
517+
:rules="[{ required: true, message: '请填写密码' }]"
518+
/>
519+
</wd-cell-group>
520+
<view class="footer">
521+
<wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
522+
</view>
523+
</wd-form>
524+
```
525+
526+
```typescript [typescript]
527+
<script lang="ts" setup>
528+
import { useToast } from '@/uni_modules/wot-design-uni'
529+
import type { FormInstance } from '@/uni_modules/wot-design-uni/components/wd-form/types'
530+
import { reactive, ref } from 'vue'
531+
532+
const { success: showSuccess } = useToast()
533+
const model = reactive<{
534+
value1: string
535+
value2: string
536+
}>({
537+
value1: '',
538+
value2: ''
539+
})
540+
})
541+
}
462542
</script>
463543
```
464544

@@ -938,7 +1018,7 @@ function handleIconClick() {
9381018

9391019
| 事件名称 | 说明 | 参数 | 最低版本 |
9401020
| -------- | ------------------------------------------------------------------------------ | --------------- | -------- |
941-
| validate | 验证表单,支持传入一个 prop 来验证单个表单项,不传入 prop 时,会验证所有表单项 | `prop?: string` | 0.2.0 |
1021+
| validate | 验证表单,支持传入一个 prop 来验证单个表单项,不传入 prop 时,会验证所有表单项,$LOWEST_VERSION$ 版本起支持传入数组 | `prop?: string\|string[]` | 0.2.0 |
9421022
| reset | 重置校验结果 | - | 0.2.0 |
9431023

9441024
## 外部样式类

src/pages/form/demo2.vue

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
<!--
2+
* @Author: weisheng
3+
* @Date: 2024-11-09 12:35:25
4+
* @LastEditTime: 2025-01-13 23:46:45
5+
* @LastEditors: weisheng
6+
* @Description:
7+
* @FilePath: /wot-design-uni/src/pages/form/demo2.vue
8+
* 记得注释
9+
-->
110
<template>
211
<page-wraper>
312
<wd-form ref="form" :model="model" :reset-on-change="false">
@@ -22,11 +31,23 @@
2231
placeholder="玛卡巴卡单号"
2332
:rules="[{ required: true, message: '请填写玛卡巴卡单号' }]"
2433
/>
34+
35+
<wd-input
36+
label="玛卡巴卡id"
37+
prop="id"
38+
label-width="100px"
39+
clearable
40+
@blur="handleBlur('id')"
41+
v-model="model.id"
42+
placeholder="玛卡巴卡id"
43+
:rules="[{ required: true, message: '请填写玛卡巴卡id' }]"
44+
/>
2545
</wd-cell-group>
2646
</wd-form>
2747

2848
<view class="footer">
29-
<wd-button type="primary" size="large" block @click="handleSubmit">提交</wd-button>
49+
<wd-button type="primary" @click="handleSubmit">提交</wd-button>
50+
<wd-button type="primary" @click="handleValidate">校验单号和ID</wd-button>
3051
</view>
3152
</page-wraper>
3253
</template>
@@ -38,9 +59,11 @@ import { reactive, ref } from 'vue'
3859
const model = reactive<{
3960
name: string
4061
phoneNumber: string
62+
id: string
4163
}>({
4264
name: '',
43-
phoneNumber: ''
65+
phoneNumber: '',
66+
id: ''
4467
})
4568
4669
const { success: showSuccess } = useToast()
@@ -50,6 +73,19 @@ function handleBlur(prop: string) {
5073
form.value!.validate(prop)
5174
}
5275
76+
function handleValidate() {
77+
form
78+
.value!.validate(['phoneNumber', 'id'])
79+
.then(({ valid }) => {
80+
if (valid) {
81+
showSuccess('校验通过')
82+
}
83+
})
84+
.catch((error) => {
85+
console.log(error, 'error')
86+
})
87+
}
88+
5389
function handleSubmit() {
5490
form
5591
.value!.validate()
@@ -66,5 +102,6 @@ function handleSubmit() {
66102
<style lang="scss" scoped>
67103
.footer {
68104
padding: 12px;
105+
display: flex;
69106
}
70107
</style>

src/pages/form/demo3.vue

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<view>
33
<page-wraper>
44
<wd-message-box />
5-
<wd-toast />
65
<wd-form ref="form" :model="model" :rules="rules">
76
<wd-cell-group custom-class="group" title="基础信息" border>
87
<wd-input
@@ -89,6 +88,15 @@
8988
<wd-switch v-model="model.switchVal" />
9089
</view>
9190
</wd-cell>
91+
<wd-input
92+
label="折扣"
93+
v-if="model.switchVal"
94+
label-width="100px"
95+
prop="discount"
96+
placeholder="请输入优惠金额"
97+
clearable
98+
v-model="model.discount"
99+
/>
92100
<wd-input
93101
label="歪比巴卜"
94102
label-width="100px"
@@ -100,7 +108,11 @@
100108
/>
101109
<wd-input label="玛卡巴卡" label-width="100px" prop="phone" placeholder="请输入玛卡巴卡" clearable v-model="model.phone" />
102110
<wd-cell title="活动图片" title-width="100px" prop="fileList">
103-
<wd-upload :file-list="model.fileList" action="https://ftf.jd.com/api/uploadImg" @change="handleFileChange"></wd-upload>
111+
<wd-upload
112+
:file-list="model.fileList"
113+
action="https://mockapi.eolink.com/zhTuw2P8c29bc981a741931bdd86eb04dc1e8fd64865cb5/upload"
114+
@change="handleFileChange"
115+
></wd-upload>
104116
</wd-cell>
105117
</wd-cell-group>
106118
<view class="tip">
@@ -144,6 +156,7 @@ const model = reactive<{
144156
phone: string
145157
read: boolean
146158
fileList: UploadFileItem[]
159+
discount: number
147160
}>({
148161
couponName: '',
149162
platform: [],
@@ -159,7 +172,8 @@ const model = reactive<{
159172
cardId: '',
160173
phone: '',
161174
read: false,
162-
fileList: []
175+
fileList: [],
176+
discount: 1
163177
})
164178
165179
const rules: FormRules = {
@@ -319,6 +333,19 @@ const rules: FormRules = {
319333
}
320334
}
321335
}
336+
],
337+
discount: [
338+
{
339+
required: true,
340+
message: '请输入优惠金额',
341+
validator: (value) => {
342+
if (value) {
343+
return Promise.resolve()
344+
} else {
345+
return Promise.reject()
346+
}
347+
}
348+
}
322349
]
323350
}
324351
@@ -397,6 +424,9 @@ function handleSubmit() {
397424
form
398425
.value!.validate()
399426
.then(({ valid, errors }) => {
427+
if (valid) {
428+
toast.success('提交成功')
429+
}
400430
console.log(valid)
401431
console.log(errors)
402432
})

src/uni_modules/wot-design-uni/components/wd-form/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
22
* @Author: weisheng
33
* @Date: 2023-12-14 11:21:58
4-
* @LastEditTime: 2024-03-18 12:50:41
4+
* @LastEditTime: 2025-01-11 13:31:20
55
* @LastEditors: weisheng
66
* @Description:
7-
* @FilePath: \wot-design-uni\src\uni_modules\wot-design-uni\components\wd-form\types.ts
7+
* @FilePath: /wot-design-uni/src/uni_modules/wot-design-uni/components/wd-form/types.ts
88
* 记得注释
99
*/
1010
import { type ComponentPublicInstance, type ExtractPropTypes, type InjectionKey, type PropType } from 'vue'
@@ -72,7 +72,7 @@ export type FormExpose = {
7272
* 表单校验
7373
* @param prop 指定校验字段
7474
*/
75-
validate: (prop?: string) => Promise<{
75+
validate: (prop?: string | Array<string>) => Promise<{
7676
valid: boolean
7777
errors: ErrorMessage[]
7878
}>

0 commit comments

Comments
 (0)