perf: 优化 nested field error

This commit is contained in:
ibuler
2021-03-18 11:39:53 +08:00
committed by 老广
parent 8c3337f581
commit 88aa17550b

View File

@@ -43,12 +43,16 @@ export default {
iFields() { iFields() {
const fields = this.fields const fields = this.fields
if (this.errors && typeof this.errors === 'object') { if (this.errors && typeof this.errors === 'object') {
for (const [name, error] of Object.entries(this.errors)) { // eslint-disable-next-line prefer-const
for (let [name, error] of Object.entries(this.errors)) {
const field = fields.find((v) => v.prop === name) const field = fields.find((v) => v.prop === name)
if (!field) { if (!field) {
continue continue
} }
this.$log.debug(`${name}: ${error}`) this.$log.debug(`${name}: ${error}`)
if (typeof error === 'object' && !Array.isArray(error)) {
error = this.objectToString(error)
}
field.attrs.error = error.toString() field.attrs.error = error.toString()
} }
} }
@@ -57,15 +61,16 @@ export default {
} }
}, },
methods: { methods: {
setFieldError(name, error) { objectToString(obj) {
const field = this.totalFields.find((v) => v.prop === name) let data = ''
if (!field) { // eslint-disable-next-line prefer-const
return for (let [key, value] of Object.entries(obj)) {
if (typeof value === 'object') {
value = this.objectToString(value)
}
data += ` ${key}: ${value} `
} }
if (field.attrs.error === error) { return data
error += '.'
}
field.attrs.error = error
} }
} }
} }