最后,我如何将CSS错误类添加到嵌套反应式表单中无效的字段中:
this.invalidControls(this.formGroup); //Call it just after submit the form
//The implementation of the function
invalidControls(name): void {
for (const field in name.controls) {
const control = name.get(field);
if(control.constructor.name == "FormArray" || control.constructor.name == "FormGroup"){
this.invalidControls(control); //If the AbstractControl is not a FormControl the funtion will be called again
}
else{
if(control.status === "INVALID"){
control.markAsTouched({ onlySelf: true });
control.markAsDirty({ onlySelf: true });
}
}
}
}
因此,如果将控件设置为
无效的
,则,
肮脏的
,它是
空的
,将添加CSS错误类。否则不会。