From 83ad871b2be2da772c401b5f74bb1ebad072784b Mon Sep 17 00:00:00 2001 From: mangogan <1261639871@qq.com> Date: Tue, 30 Jul 2019 14:27:28 +0800 Subject: [PATCH] fix: validate nesting item --- demo/components/formDemo.vue | 27 +++++++++++++---------- packages/common/Form/src/main.vue | 36 +++++++++++++++++++------------ 2 files changed, 38 insertions(+), 25 deletions(-) diff --git a/demo/components/formDemo.vue b/demo/components/formDemo.vue index 6de0a3157..205c6c780 100644 --- a/demo/components/formDemo.vue +++ b/demo/components/formDemo.vue @@ -281,16 +281,21 @@ ResetForm Method: only can reset the item with prop.
ValidateForm Method: support validate specified items by the second parameter in form of array.
- - - + + + Test nesting formItem in resetForm + { - cb = valid => valid ? resolve(valid) : reject(valid) + cb = (valid) => valid ? resolve(valid) : reject(valid) }) } let valid = true let fields = {} - this.$children.forEach((child, i) => { + for (let i = 0; i < target.$children.length; i++) { + let child = target.$children[i] + let componentName = child.$options.name let flag = scope.length > 0 ? scope.indexOf(child.prop) > -1 : true - if (child.prop && flag) { + if (componentName === 'NFormItem' && child.prop && flag) { child.validate('', (errors, field) => { if (errors) { valid = false } fields = Object.assign({}, fields, field) }) + } else if (['NFormItem', 'NForm'].indexOf(componentName) === -1) { + this.validate(null, [], child) } - if (++i === this.$children.length && isCallback) { + if (i === target.$children.length - 1 && isCallback) { cb(valid, fields) } - }) + } if (promise) { return promise @@ -100,24 +104,28 @@ export default { /** * just can reset the value with prop in form-item */ - resetForm () { - this.$children.forEach(child => { - if (child.prop) { + resetForm (target = this) { + for (let i = 0; i < target.$children.length; i++) { + let child = target.$children[i] + let componentName = child.$options.name + if (componentName === 'NFormItem' && child.prop) { let keys = child.prop.split('.') let obj = this.model let j = 0 - keys.forEach((k, i) => { - if (i !== keys.length - 1) { - obj = obj[k] + keys.forEach((m, n) => { + if (n !== keys.length - 1) { + obj = obj[m] } - j = i + j = n }) obj[keys[j]] = getObjValue(this.initialValue, keys) if (child.validateFlag) { child.clearValidateClass() } + } else if (['NFormItem', 'NForm'].indexOf(componentName) === -1) { + this.resetForm(child) } - }) + } } } }