1
1
<template >
2
- <el-form-item label =" 枚举项" prop =" enum" >
2
+ <el-form-item
3
+ :rules =" [{ required: true, validator: validateEnumList, trigger: 'change' }]"
4
+ label =" 枚举项"
5
+ prop =" property.dataSpecsList"
6
+ >
3
7
<div class =" flex flex-col" >
4
8
<div class =" flex items-center" >
5
9
<span class =" flex-1" > 参数值 </span >
10
14
:key =" index"
11
15
class =" flex items-center justify-between mb-5px"
12
16
>
13
- <el-input v-model =" item.value" placeholder =" 请输入枚举值,如'0'" />
17
+ <el-form-item
18
+ :prop =" `property.dataSpecsList[${index}].value`"
19
+ :rules =" [
20
+ { required: true, message: '枚举值不能为空' },
21
+ { validator: validateEnumValue, trigger: 'blur' }
22
+ ]"
23
+ class =" flex-1 mb-0"
24
+ >
25
+ <el-input v-model =" item.value" placeholder =" 请输入枚举值,如'0'" />
26
+ </el-form-item >
14
27
<span class =" mx-2" >~</span >
15
- <el-input v-model =" item.name" placeholder =" 对该枚举项的描述" />
16
- <el-button link type =" primary" class =" ml-10px" @click =" deleteEnum(index)" >删除</el-button >
28
+ <el-form-item
29
+ :prop =" `property.dataSpecsList[${index}].name`"
30
+ :rules =" [
31
+ { required: true, message: '枚举描述不能为空' },
32
+ { validator: validateEnumName, trigger: 'blur' }
33
+ ]"
34
+ class =" flex-1 mb-0"
35
+ >
36
+ <el-input v-model =" item.name" placeholder =" 对该枚举项的描述" />
37
+ </el-form-item >
38
+ <el-button class =" ml-10px" link type =" primary" @click =" deleteEnum(index)" >删除</el-button >
17
39
</div >
18
40
<el-button link type =" primary" @click =" addEnum" >+添加枚举项</el-button >
19
41
</div >
@@ -26,6 +48,7 @@ import {
26
48
DataSpecsDataType ,
27
49
DataSpecsEnumOrBoolDataVO
28
50
} from ' @/views/iot/product/product/detail/ThingModel/config'
51
+ import { isEmpty } from ' @/utils/is'
29
52
30
53
/** 枚举型的 dataSpecs 配置组件 */
31
54
defineOptions ({ name: ' ThingModelEnumTypeDataSpecs' })
@@ -51,6 +74,93 @@ const deleteEnum = (index: number) => {
51
74
}
52
75
dataSpecsList .value .splice (index , 1 )
53
76
}
77
+
78
+ /** 校验枚举值 */
79
+ const validateEnumValue = (_ : any , value : any , callback : any ) => {
80
+ if (isEmpty (value )) {
81
+ callback (new Error (' 枚举值不能为空' ))
82
+ return
83
+ }
84
+ if (isNaN (Number (value ))) {
85
+ callback (new Error (' 枚举值必须是数字' ))
86
+ return
87
+ }
88
+ // 检查枚举值是否重复
89
+ const values = dataSpecsList .value .map ((item ) => item .value )
90
+ if (values .filter ((v ) => v === value ).length > 1 ) {
91
+ callback (new Error (' 枚举值不能重复' ))
92
+ return
93
+ }
94
+ callback ()
95
+ }
96
+
97
+ /** 校验枚举描述 */
98
+ const validateEnumName = (_ : any , value : string , callback : any ) => {
99
+ if (isEmpty (value )) {
100
+ callback (new Error (' 枚举描述不能为空' ))
101
+ return
102
+ }
103
+
104
+ // 检查开头字符
105
+ if (! / ^ [\u4e00 -\u9fa5 a-zA-Z0-9 ] / .test (value )) {
106
+ callback (new Error (' 枚举描述必须以中文、英文字母或数字开头' ))
107
+ return
108
+ }
109
+
110
+ // 检查整体格式
111
+ if (! / ^ [\u4e00 -\u9fa5 a-zA-Z0-9 ][a-zA-Z0-9\u4e00 -\u9fa5 _-] * $ / .test (value )) {
112
+ callback (new Error (' 枚举描述只能包含中文、英文字母、数字、下划线和短划线' ))
113
+ return
114
+ }
115
+
116
+ // 检查长度(一个中文算一个字符)
117
+ if (value .length > 20 ) {
118
+ callback (new Error (' 枚举描述长度不能超过20个字符' ))
119
+ return
120
+ }
121
+
122
+ callback ()
123
+ }
124
+
125
+ /** 校验整个枚举列表 */
126
+ const validateEnumList = (_ : any , __ : any , callback : any ) => {
127
+ if (isEmpty (dataSpecsList .value )) {
128
+ callback (new Error (' 请至少添加一个枚举项' ))
129
+ return
130
+ }
131
+
132
+ // 检查是否存在空值
133
+ const hasEmptyValue = dataSpecsList .value .some (
134
+ (item ) => isEmpty (item .value ) || isEmpty (item .name )
135
+ )
136
+ if (hasEmptyValue ) {
137
+ callback (new Error (' 存在未填写的枚举值或描述' ))
138
+ return
139
+ }
140
+
141
+ // 检查枚举值是否都是数字
142
+ const hasInvalidNumber = dataSpecsList .value .some ((item ) => isNaN (Number (item .value )))
143
+ if (hasInvalidNumber ) {
144
+ callback (new Error (' 存在非数字的枚举值' ))
145
+ return
146
+ }
147
+
148
+ // 检查是否有重复的枚举值
149
+ const values = dataSpecsList .value .map ((item ) => item .value )
150
+ const uniqueValues = new Set (values )
151
+ if (values .length !== uniqueValues .size ) {
152
+ callback (new Error (' 存在重复的枚举值' ))
153
+ return
154
+ }
155
+
156
+ callback ()
157
+ }
54
158
</script >
55
159
56
- <style lang="scss" scoped></style >
160
+ <style lang="scss" scoped>
161
+ :deep(.el-form-item ) {
162
+ .el-form-item {
163
+ margin-bottom : 0 ;
164
+ }
165
+ }
166
+ </style >
0 commit comments