Managing around 50 controls on my page, especially when it comes to validation, is a bit overwhelming. I find myself repeating the same Validators
for each control.
I'm considering grouping the validators into two categories:
1. [Validators.required, Validators.maxLength(50)]
2. Validators.required
However, using the standard validation method makes my code lengthy. Is there a way I can utilize 2 FormArray
to organize the controls and apply validations?
this.candidateForm = new FormGroup({
firstName: new FormControl('', [Validators.required, Validators.maxLength(50)]),
lastName: new FormControl('', [Validators.required, Validators.maxLength(50)]),
fatherName: new FormControl('', [Validators.required, Validators.maxLength(50)]),
motherName: new FormControl('', [Validators.required, Validators.maxLength(50)]),
placeOfBirth: new FormControl('', Validators.required),
nationalityList: new FormControl('', Validators.required),
dateOfBirth: new FormControl('', [Validators.required, Validators.maxLength(50)]),
genderList: new FormControl('', Validators.required),
});
Is there a way to make this code more readable?
For instance,
FormArray[] values = formControl{ control1, control2, control3};
FormArray[] values2 = formControl{ control4, control5, control6};
validation1 = [Validators.required, Validators.maxLength(50)]
validation2 = [Validators.required]
values = Validation1
values2 = Validation2
This approach would simplify adding additional controls in the future.