Skip to content

Commit 0e6ea3e

Browse files
author
xuqingkai
committed
docs: ✏️ 补充Form设置error-type的示例
1 parent 592c37b commit 0e6ea3e

File tree

4 files changed

+122
-37
lines changed

4 files changed

+122
-37
lines changed

docs/component/form.md

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const model = reactive<{
137137

138138
const form = ref()
139139

140-
function handleSubmit1() {
140+
function handleSubmit() {
141141
form.value
142142
.validate()
143143
.then(({ valid, errors }) => {
@@ -154,7 +154,6 @@ function handleSubmit1() {
154154
</script>
155155
```
156156

157-
158157
:::
159158

160159
## 校验规则
@@ -403,62 +402,59 @@ const submit = () => {
403402
::: code-group
404403

405404
```html [vue]
406-
<wd-form ref="form" :model="model">
405+
<wd-form ref="form" :model="model" errorType="toast">
407406
<wd-cell-group border>
408407
<wd-input
409408
label="用户名"
410409
label-width="100px"
411-
prop="name"
410+
prop="value1"
412411
clearable
413-
v-model="model.name"
412+
v-model="model.value1"
414413
placeholder="请输入用户名"
415-
@blur="handleBlur('name')"
416414
:rules="[{ required: true, message: '请填写用户名' }]"
417415
/>
418416
<wd-input
419-
label="联系方式"
420-
prop="phoneNumber"
417+
label="密码"
421418
label-width="100px"
419+
prop="value2"
420+
show-password
422421
clearable
423-
@blur="handleBlur('phoneNumber')"
424-
v-model="model.phoneNumber"
425-
placeholder="联系方式"
426-
:rules="[{ required: true, message: '请填写联系方式' }]"
422+
v-model="model.value2"
423+
placeholder="请输入密码"
424+
:rules="[{ required: true, message: '请填写密码' }]"
427425
/>
428426
</wd-cell-group>
427+
<view class="footer">
428+
<wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
429+
</view>
429430
</wd-form>
430-
431-
<view class="footer">
432-
<wd-button type="primary" size="large" block @click="handleSubmit">提交</wd-button>
433-
</view>
434431
```
435432

436433
```typescript [typescript]
437434
<script lang="ts" setup>
438435
import { useToast } from '@/uni_modules/wot-design-uni'
436+
import type { FormInstance } from '@/uni_modules/wot-design-uni/components/wd-form/types'
439437
import { reactive, ref } from 'vue'
440438

439+
const { success: showSuccess } = useToast()
441440
const model = reactive<{
442-
name: string
443-
phoneNumber: string
441+
value1: string
442+
value2: string
444443
}>({
445-
name: '',
446-
phoneNumber: ''
444+
value1: '',
445+
value2: ''
447446
})
448447

449-
const { success: showSuccess } = useToast()
450-
const form = ref()
451-
452-
function handleBlur(prop: string) {
453-
form.value.validate(prop)
454-
}
448+
const form = ref<FormInstance>()
455449

456450
function handleSubmit() {
457-
form.value
458-
.validate()
459-
.then(({ valid }) => {
451+
form
452+
.value!.validate()
453+
.then(({ valid, errors }) => {
460454
if (valid) {
461-
showSuccess('校验通过')
455+
showSuccess({
456+
msg: '校验通过'
457+
})
462458
}
463459
})
464460
.catch((error) => {
@@ -573,7 +569,15 @@ function handleSubmit() {
573569
<wd-switch v-model="model.switchVal" />
574570
</view>
575571
</wd-cell>
576-
<wd-input label="歪比巴卜" label-width="100px" prop="cardId" suffix-icon="camera" placeholder="请输入歪比巴卜" clearable v-model="model.cardId" />
572+
<wd-input
573+
label="歪比巴卜"
574+
label-width="100px"
575+
prop="cardId"
576+
suffix-icon="camera"
577+
placeholder="请输入歪比巴卜"
578+
clearable
579+
v-model="model.cardId"
580+
/>
577581
<wd-input label="玛卡巴卡" label-width="100px" prop="phone" placeholder="请输入玛卡巴卡" clearable v-model="model.phone" />
578582
<wd-cell title="活动图片" title-width="100px" prop="fileList">
579583
<wd-upload :file-list="model.fileList" action="https://ftf.jd.com/api/uploadImg" @change="handleFileChange"></wd-upload>
@@ -916,12 +920,12 @@ function handleIconClick() {
916920

917921
## Attributes
918922

919-
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
920-
| ----- | ------------ | --------------------- | ------ | ------ | -------- |
921-
| model | 表单数据对象 | `Record<string, any>` | - | - | 0.2.0 |
922-
| rules | 表单验证规则 | `FormRules` | - | - | 0.2.0 |
923-
| resetOnChange | 表单数据变化时是否重置表单提示信息(设置为false时需要开发者单独对变更项进行校验| `boolean` | - | `true` | 0.2.16 |
924-
| errorType | 校验错误提示方式 | `toast/message/none` | - | `message` | $LOWEST_VERSION$ |
923+
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
924+
| ------------- | ----------------------------------------------------------------------------------- | --------------------- | ------ | --------- | ---------------- |
925+
| model | 表单数据对象 | `Record<string, any>` | - | - | 0.2.0 |
926+
| rules | 表单验证规则 | `FormRules` | - | - | 0.2.0 |
927+
| resetOnChange | 表单数据变化时是否重置表单提示信息(设置为 false 时需要开发者单独对变更项进行校验| `boolean` | - | `true` | 0.2.16 |
928+
| errorType | 校验错误提示方式 | `toast/message/none` | - | `message` | $LOWEST_VERSION$ |
925929

926930
### FormItemRule 数据结构
927931

src/pages.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,16 @@
712712
"navigationBarTitleText": "Sidebar 自定义图标"
713713
}
714714
},
715+
{
716+
"path": "pages/form/demo4",
717+
"name": "formDemo4",
718+
"style": {
719+
"mp-alipay": {
720+
"allowsBounceVertical": "NO"
721+
},
722+
"navigationBarTitleText": "校验提示方式"
723+
}
724+
},
715725
{
716726
"path": "pages/fab/Index",
717727
"name": "fab",

src/pages/form/Index.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@
104104
<wd-button @click="handleClick3" :round="false" block size="large">复杂表单</wd-button>
105105
</view>
106106
</demo-block>
107+
108+
<demo-block title="校验提示方式" transparent>
109+
<view class="demo-button">
110+
<wd-button @click="handleClick4" :round="false" block size="large">校验提示方式</wd-button>
111+
</view>
112+
</demo-block>
107113
</page-wraper>
108114
</template>
109115
<script lang="ts" setup>
@@ -199,6 +205,10 @@ function handleClick2() {
199205
function handleClick3() {
200206
uni.navigateTo({ url: '/pages/form/demo3' })
201207
}
208+
209+
function handleClick4() {
210+
uni.navigateTo({ url: '/pages/form/demo4' })
211+
}
202212
</script>
203213
<style lang="scss" scoped>
204214
.demo-button {

src/pages/form/demo4.vue

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<template>
2+
<page-wraper>
3+
<wd-form ref="form" :model="model" errorType="toast">
4+
<wd-cell-group border>
5+
<wd-input
6+
label="用户名"
7+
label-width="100px"
8+
prop="value1"
9+
clearable
10+
v-model="model.value1"
11+
placeholder="请输入用户名"
12+
:rules="[{ required: true, message: '请填写用户名' }]"
13+
/>
14+
<wd-input
15+
label="密码"
16+
label-width="100px"
17+
prop="value2"
18+
show-password
19+
clearable
20+
v-model="model.value2"
21+
placeholder="请输入密码"
22+
:rules="[{ required: true, message: '请填写密码' }]"
23+
/>
24+
</wd-cell-group>
25+
<view class="footer">
26+
<wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
27+
</view>
28+
</wd-form>
29+
</page-wraper>
30+
</template>
31+
<script lang="ts" setup>
32+
import { useToast } from '@/uni_modules/wot-design-uni'
33+
import type { FormInstance } from '@/uni_modules/wot-design-uni/components/wd-form/types'
34+
import { reactive, ref } from 'vue'
35+
36+
const { success: showSuccess } = useToast()
37+
const model = reactive<{
38+
value1: string
39+
value2: string
40+
}>({
41+
value1: '',
42+
value2: ''
43+
})
44+
45+
const form = ref<FormInstance>()
46+
47+
function handleSubmit() {
48+
form
49+
.value!.validate()
50+
.then(({ valid, errors }) => {
51+
if (valid) {
52+
showSuccess({
53+
msg: '校验通过'
54+
})
55+
}
56+
})
57+
.catch((error) => {
58+
console.log(error, 'error')
59+
})
60+
}
61+
</script>

0 commit comments

Comments
 (0)