浏览代码

fix: form validate, close #510 (#513)

pointhalo 3 年之前
父节点
当前提交
bde7431268
共有 2 个文件被更改,包括 15 次插入6 次删除
  1. 14 5
      packages/semi-foundation/form/foundation.ts
  2. 1 1
      packages/semi-ui/form/_story/Validate/validateDemo.jsx

+ 14 - 5
packages/semi-foundation/form/foundation.ts

@@ -258,10 +258,19 @@ export default class FormFoundation extends BaseFoundation<BaseFormAdapter> {
             });
             });
     }
     }
 
 
-    // All fields: a[0]、a[1]、b.type、b.name[2]、b.name[0]
-    // input => output:
-    //      a  => a[0]、a[1]
-    //      b  => b.type、b.name[3]、b.name[2]
+    /**
+     * Case A:
+     *      All fields: a[0]、a[1]、b.type、b.name[2]、b.name[0]
+     *      input => output:
+     *           a  => a[0]、a[1]
+     *           b  => b.type、b.name[0]、b.name[2]
+     *
+     * Case B:
+     *      All fields: activity.a[0]、activity.a[1]、activity.c、activity.d、other
+     *      input => output:
+     *           activity.a => activity.a[0]、activity.a[1]
+     *
+     */
     _getNestedField(path: string): Map<string, FieldStaff> {
     _getNestedField(path: string): Map<string, FieldStaff> {
         const allRegisterField = this.fields;
         const allRegisterField = this.fields;
         const allFieldPath = [...allRegisterField].map(item => item[0]);
         const allFieldPath = [...allRegisterField].map(item => item[0]);
@@ -269,7 +278,7 @@ export default class FormFoundation extends BaseFoundation<BaseFormAdapter> {
         allFieldPath.forEach(item => {
         allFieldPath.forEach(item => {
             let itemPath = toPath(item);
             let itemPath = toPath(item);
             let targetPath = toPath(path);
             let targetPath = toPath(path);
-            if (itemPath[0] === targetPath[0]) {
+            if (targetPath.every((path, i) => (targetPath[i] === itemPath[i]))) {
                 const realField = allRegisterField.get(item);
                 const realField = allRegisterField.get(item);
                 nestedFieldPath.set(item, realField);
                 nestedFieldPath.set(item, realField);
             }
             }

+ 1 - 1
packages/semi-ui/form/_story/Validate/validateDemo.jsx

@@ -201,7 +201,7 @@ class PartValidAndResetDemo extends Component {
     }
     }
 
 
     render() {
     render() {
-        let options = ['a', 'b', 'c', 'd'].map(item => ({ label: item, value: item }));
+        let options = ['a', 'b', 'c', 'd', 'b.name'].map(item => ({ label: item, value: item }));
         return (
         return (
             <Form getFormApi={this.getFormApi} autoScrollToError>
             <Form getFormApi={this.getFormApi} autoScrollToError>
                 <Input field="a[1]" validate={this.validate} trigger="blur" />
                 <Input field="a[1]" validate={this.validate} trigger="blur" />