|
17 | 17 | :label="field.label" |
18 | 18 | :prop="field.model" |
19 | 19 | :required="field.required" |
20 | | - :rules="getRules(field)" |
| 20 | + :rules="getRules" |
21 | 21 | :label-width="field.labelWidth" |
22 | 22 | :class="itemClasses" |
23 | 23 | :style="itemStyle" |
@@ -161,6 +161,77 @@ export default { |
161 | 161 | contentShow() { |
162 | 162 | let content = this.field.labelTip && this.field.labelTip.content || {}; |
163 | 163 | return content.ifShow; |
| 164 | + }, |
| 165 | + getRules() { |
| 166 | + const field = this.field; |
| 167 | + const type = field.type.toLowerCase(); |
| 168 | + const subtype = field.subtype; |
| 169 | +
|
| 170 | + let rules = []; |
| 171 | + if (field.required) { |
| 172 | + if (type === 'datepicker' && ['daterange', 'datetimerange'].includes(subtype)) { |
| 173 | + rules.push({ |
| 174 | + validator(rule, value, callback) { |
| 175 | + if (value.length === 2 && value[0] && value[1]) { |
| 176 | + callback(); |
| 177 | + } |
| 178 | + else { |
| 179 | + callback(new Error(field.label + '不可为空')); |
| 180 | + } |
| 181 | + }, |
| 182 | + trigger: 'change' |
| 183 | + }); |
| 184 | + } |
| 185 | + if (type === 'timepicker' && ['timerange'].includes(subtype)) { |
| 186 | + rules.push({ |
| 187 | + validator(rule, value, callback) { |
| 188 | + if (value.length === 2 && value[0] && value[1]) { |
| 189 | + callback(); |
| 190 | + } |
| 191 | + else { |
| 192 | + callback(new Error(field.label + '不可为空')); |
| 193 | + } |
| 194 | + }, |
| 195 | + trigger: 'change' |
| 196 | + }); |
| 197 | + } |
| 198 | + if (['logicinput', 'logicselect'].includes(type)) { |
| 199 | + rules.push({ |
| 200 | + validator(rule, value, callback) { |
| 201 | + if (value.logic && value.value) { |
| 202 | + callback(); |
| 203 | + } |
| 204 | + else { |
| 205 | + callback(new Error(field.label + '不可为空')); |
| 206 | + } |
| 207 | + }, |
| 208 | + trigger: 'change' |
| 209 | + }); |
| 210 | + } |
| 211 | + rules.push({ |
| 212 | + required: true, |
| 213 | + type: getValidType(field), |
| 214 | + message: (field.label || field.model) + '不可为空', |
| 215 | + trigger: 'change' |
| 216 | + }); |
| 217 | + } |
| 218 | + if (field.rules) { |
| 219 | + const model = this.form.model; |
| 220 | + const validateValue = Object.assign({}, model || {}, this.paramsContainer || {}); |
| 221 | + if (Object.prototype.toString.call(field.rules) === '[object Array]') { |
| 222 | + rules = rules.concat(field.rules); |
| 223 | + } else { |
| 224 | + Object.keys(field.rules).map(model => { |
| 225 | + field.rules[model].map(field => { |
| 226 | + if (validateValue[model] === field.value) { |
| 227 | + rules = rules.concat(field.rules); |
| 228 | +
|
| 229 | + } |
| 230 | + }); |
| 231 | + }); |
| 232 | + } |
| 233 | + } |
| 234 | + return rules; |
164 | 235 | } |
165 | 236 | }, |
166 | 237 | created() { |
@@ -228,62 +299,7 @@ export default { |
228 | 299 | handelListItemClick(value) { |
229 | 300 | this.$emit('on-list-item-click', value); |
230 | 301 | }, |
231 | | - getRules(field) { |
232 | | - const type = field.type.toLowerCase(); |
233 | | - const subtype = field.subtype; |
234 | | - let rules = []; |
235 | | - if (field.required) { |
236 | | - if (type === 'datepicker' && ['daterange', 'datetimerange'].includes(subtype)) { |
237 | | - rules.push({ |
238 | | - validator(rule, value, callback) { |
239 | | - if (value.length === 2 && value[0] && value[1]) { |
240 | | - callback(); |
241 | | - } |
242 | | - else { |
243 | | - callback(new Error(field.label + '不可为空')); |
244 | | - } |
245 | | - }, |
246 | | - trigger: 'change' |
247 | | - }); |
248 | | - } |
249 | | - if (type === 'timepicker' && ['timerange'].includes(subtype)) { |
250 | | - rules.push({ |
251 | | - validator(rule, value, callback) { |
252 | | - if (value.length === 2 && value[0] && value[1]) { |
253 | | - callback(); |
254 | | - } |
255 | | - else { |
256 | | - callback(new Error(field.label + '不可为空')); |
257 | | - } |
258 | | - }, |
259 | | - trigger: 'change' |
260 | | - }); |
261 | | - } |
262 | | - if (['logicinput', 'logicselect'].includes(type)) { |
263 | | - rules.push({ |
264 | | - validator(rule, value, callback) { |
265 | | - if (value.logic && value.value) { |
266 | | - callback(); |
267 | | - } |
268 | | - else { |
269 | | - callback(new Error(field.label + '不可为空')); |
270 | | - } |
271 | | - }, |
272 | | - trigger: 'change' |
273 | | - }); |
274 | | - } |
275 | | - rules.push({ |
276 | | - required: true, |
277 | | - type: getValidType(field), |
278 | | - message: (field.label || field.model) + '不可为空', |
279 | | - trigger: 'change' |
280 | | - }); |
281 | | - } |
282 | | - if (field.rules) { |
283 | | - rules = rules.concat(field.rules); |
284 | | - } |
285 | | - return rules; |
286 | | - }, |
| 302 | + |
287 | 303 |
|
288 | 304 | submit(component) { |
289 | 305 | let field = component.field; |
|
0 commit comments