根据嵌套表格组中的条件清除验证

时间:2019-05-29 13:44:58

标签: angular6

我正在使用动态表单生成,其中我需要基于嵌套表单组中的某些条件来维护验证,如果条件匹配,我可以添加所需的验证器,之后如果条件更改,则需要删除该字段中的需求验证器

我使用了一个函数来设置字段的验证,该函数递归地检查控件并添加验证

        const condition = field.condition;
        const operator = condition.operator ? condition.operator : '=';
        let show;
        const page = this.formConfiguration.pages.find(findpage => findpage.pageId === condition.question.page);
        const section = page.sections.find(findsection => findsection.sectionId === condition.question.section);
        const question = section.questions.find(findQuestion => findQuestion.id === condition.question.question);
        show = this.comparators[operator](form.value[question.name], condition.value);
        if (field.childValidation) {
            if (show && field.validations.length === 0 && !field.renderChild) {
                field.validations.push({ name: 'required', message: 'Required' });
                field.renderChild = true;
                this.updateValidations(this.form, field.name, field.childValidation);
            } else {
                if (field.renderChild && field.validations.length !== 0) {
                    field.validations = [];
                    field.renderChild = false;
                }
            }
        }
        return show;
    }

    updateValidations(formGroup: FormGroup, formControlName, childValidation) {
        if (childValidation) {
            Object.keys(formGroup.controls).forEach(field => {
                console.log(formControlName === field, 'TIMES');
                if (formControlName === field) {
                    const control = formGroup.get(field);
                    control.setValidators([Validators.required]);
                }
                // Recurrsion over inner formgroup of formgroup
                if ('controls' in formGroup.controls[field]) {
                    this.updateValidations((formGroup.controls[field] as FormGroup), formControlName, childValidation);
                }
            });
        }
    }```

here the field json which generate the form control 

``` {
                name: 'Question2',
                label: 'Upto which standard :  *',
                type: 'input',
                inputType: 'text',
                validations: [
                ],
                icon: 'text_fields',
                tooltip: 'Text Input',
                id: 421,
                condition: {
                  question: {
                    page: 878,
                    section: 428,
                    question: 950
                  },
                  value: 'If enrolled then write up to which std. studied'
                }
              }
            ],
          }```

in the above code, the application is slowing down. the code is not optimized . in above, a field which is JSON object from which control is being generated. there are one property validators for that field which is an array. I need to update that validation property of that field based on the condition. if the condition is true then push the array. After that, if any changes accrue then again check on that field for the condition if the condition fails then pop the validation.

0 个答案:

没有答案