docs: ✏️ 补充Form设置error-type的示例

This commit is contained in:
xuqingkai 2024-08-08 14:03:44 +08:00
parent 592c37b15e
commit 0e6ea3ead0
4 changed files with 122 additions and 37 deletions

View File

@ -137,7 +137,7 @@ const model = reactive<{
const form = ref() const form = ref()
function handleSubmit1() { function handleSubmit() {
form.value form.value
.validate() .validate()
.then(({ valid, errors }) => { .then(({ valid, errors }) => {
@ -154,7 +154,6 @@ function handleSubmit1() {
</script> </script>
``` ```
::: :::
## 校验规则 ## 校验规则
@ -403,62 +402,59 @@ const submit = () => {
::: code-group ::: code-group
```html [vue] ```html [vue]
<wd-form ref="form" :model="model"> <wd-form ref="form" :model="model" errorType="toast">
<wd-cell-group border> <wd-cell-group border>
<wd-input <wd-input
label="用户名" label="用户名"
label-width="100px" label-width="100px"
prop="name" prop="value1"
clearable clearable
v-model="model.name" v-model="model.value1"
placeholder="请输入用户名" placeholder="请输入用户名"
@blur="handleBlur('name')"
:rules="[{ required: true, message: '请填写用户名' }]" :rules="[{ required: true, message: '请填写用户名' }]"
/> />
<wd-input <wd-input
label="联系方式" label="密码"
prop="phoneNumber"
label-width="100px" label-width="100px"
prop="value2"
show-password
clearable clearable
@blur="handleBlur('phoneNumber')" v-model="model.value2"
v-model="model.phoneNumber" placeholder="请输入密码"
placeholder="联系方式" :rules="[{ required: true, message: '请填写密码' }]"
:rules="[{ required: true, message: '请填写联系方式' }]"
/> />
</wd-cell-group> </wd-cell-group>
<view class="footer">
<wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
</view>
</wd-form> </wd-form>
<view class="footer">
<wd-button type="primary" size="large" block @click="handleSubmit">提交</wd-button>
</view>
``` ```
```typescript [typescript] ```typescript [typescript]
<script lang="ts" setup> <script lang="ts" setup>
import { useToast } from '@/uni_modules/wot-design-uni' import { useToast } from '@/uni_modules/wot-design-uni'
import type { FormInstance } from '@/uni_modules/wot-design-uni/components/wd-form/types'
import { reactive, ref } from 'vue' import { reactive, ref } from 'vue'
const { success: showSuccess } = useToast()
const model = reactive<{ const model = reactive<{
name: string value1: string
phoneNumber: string value2: string
}>({ }>({
name: '', value1: '',
phoneNumber: '' value2: ''
}) })
const { success: showSuccess } = useToast() const form = ref<FormInstance>()
const form = ref()
function handleBlur(prop: string) {
form.value.validate(prop)
}
function handleSubmit() { function handleSubmit() {
form.value form
.validate() .value!.validate()
.then(({ valid }) => { .then(({ valid, errors }) => {
if (valid) { if (valid) {
showSuccess('校验通过') showSuccess({
msg: '校验通过'
})
} }
}) })
.catch((error) => { .catch((error) => {
@ -573,7 +569,15 @@ function handleSubmit() {
<wd-switch v-model="model.switchVal" /> <wd-switch v-model="model.switchVal" />
</view> </view>
</wd-cell> </wd-cell>
<wd-input label="歪比巴卜" label-width="100px" prop="cardId" suffix-icon="camera" placeholder="请输入歪比巴卜" clearable v-model="model.cardId" /> <wd-input
label="歪比巴卜"
label-width="100px"
prop="cardId"
suffix-icon="camera"
placeholder="请输入歪比巴卜"
clearable
v-model="model.cardId"
/>
<wd-input label="玛卡巴卡" label-width="100px" prop="phone" placeholder="请输入玛卡巴卡" clearable v-model="model.phone" /> <wd-input label="玛卡巴卡" label-width="100px" prop="phone" placeholder="请输入玛卡巴卡" clearable v-model="model.phone" />
<wd-cell title="活动图片" title-width="100px" prop="fileList"> <wd-cell title="活动图片" title-width="100px" prop="fileList">
<wd-upload :file-list="model.fileList" action="https://ftf.jd.com/api/uploadImg" @change="handleFileChange"></wd-upload> <wd-upload :file-list="model.fileList" action="https://ftf.jd.com/api/uploadImg" @change="handleFileChange"></wd-upload>
@ -916,12 +920,12 @@ function handleIconClick() {
## Attributes ## Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 | | 参数 | 说明 | 类型 | 可选值 | 默认值 | 最低版本 |
| ----- | ------------ | --------------------- | ------ | ------ | -------- | | ------------- | ----------------------------------------------------------------------------------- | --------------------- | ------ | --------- | ---------------- |
| model | 表单数据对象 | `Record<string, any>` | - | - | 0.2.0 | | model | 表单数据对象 | `Record<string, any>` | - | - | 0.2.0 |
| rules | 表单验证规则 | `FormRules` | - | - | 0.2.0 | | rules | 表单验证规则 | `FormRules` | - | - | 0.2.0 |
| resetOnChange | 表单数据变化时是否重置表单提示信息设置为false时需要开发者单独对变更项进行校验 | `boolean` | - | `true` | 0.2.16 | | resetOnChange | 表单数据变化时是否重置表单提示信息(设置为 false 时需要开发者单独对变更项进行校验) | `boolean` | - | `true` | 0.2.16 |
| errorType | 校验错误提示方式 | `toast/message/none` | - | `message` | $LOWEST_VERSION$ | | errorType | 校验错误提示方式 | `toast/message/none` | - | `message` | $LOWEST_VERSION$ |
### FormItemRule 数据结构 ### FormItemRule 数据结构

View File

@ -712,6 +712,16 @@
"navigationBarTitleText": "Sidebar 自定义图标" "navigationBarTitleText": "Sidebar 自定义图标"
} }
}, },
{
"path": "pages/form/demo4",
"name": "formDemo4",
"style": {
"mp-alipay": {
"allowsBounceVertical": "NO"
},
"navigationBarTitleText": "校验提示方式"
}
},
{ {
"path": "pages/fab/Index", "path": "pages/fab/Index",
"name": "fab", "name": "fab",

View File

@ -104,6 +104,12 @@
<wd-button @click="handleClick3" :round="false" block size="large">复杂表单</wd-button> <wd-button @click="handleClick3" :round="false" block size="large">复杂表单</wd-button>
</view> </view>
</demo-block> </demo-block>
<demo-block title="校验提示方式" transparent>
<view class="demo-button">
<wd-button @click="handleClick4" :round="false" block size="large">校验提示方式</wd-button>
</view>
</demo-block>
</page-wraper> </page-wraper>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
@ -199,6 +205,10 @@ function handleClick2() {
function handleClick3() { function handleClick3() {
uni.navigateTo({ url: '/pages/form/demo3' }) uni.navigateTo({ url: '/pages/form/demo3' })
} }
function handleClick4() {
uni.navigateTo({ url: '/pages/form/demo4' })
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.demo-button { .demo-button {

61
src/pages/form/demo4.vue Normal file
View File

@ -0,0 +1,61 @@
<template>
<page-wraper>
<wd-form ref="form" :model="model" errorType="toast">
<wd-cell-group border>
<wd-input
label="用户名"
label-width="100px"
prop="value1"
clearable
v-model="model.value1"
placeholder="请输入用户名"
:rules="[{ required: true, message: '请填写用户名' }]"
/>
<wd-input
label="密码"
label-width="100px"
prop="value2"
show-password
clearable
v-model="model.value2"
placeholder="请输入密码"
:rules="[{ required: true, message: '请填写密码' }]"
/>
</wd-cell-group>
<view class="footer">
<wd-button type="primary" size="large" @click="handleSubmit" block>提交</wd-button>
</view>
</wd-form>
</page-wraper>
</template>
<script lang="ts" setup>
import { useToast } from '@/uni_modules/wot-design-uni'
import type { FormInstance } from '@/uni_modules/wot-design-uni/components/wd-form/types'
import { reactive, ref } from 'vue'
const { success: showSuccess } = useToast()
const model = reactive<{
value1: string
value2: string
}>({
value1: '',
value2: ''
})
const form = ref<FormInstance>()
function handleSubmit() {
form
.value!.validate()
.then(({ valid, errors }) => {
if (valid) {
showSuccess({
msg: '校验通过'
})
}
})
.catch((error) => {
console.log(error, 'error')
})
}
</script>