I have a form where are sub categories and save button calls mat-error correctly for all the fields in all categories in once correctly if nothing entered. But I also have add button for each category and want to validate if just that fields have value before adding new fields?
-
Could you please provide some code on stackblitz.com to reproduce your issue?Merna Mustafa– Merna Mustafa2022-02-22 09:01:59 +00:00Commented Feb 22, 2022 at 9:01
-
Please provide enough code so others can better understand or reproduce the problem.Community– Community Bot2022-03-06 07:38:12 +00:00Commented Mar 6, 2022 at 7:38
Add a comment
|
1 Answer
I imagine you has a FormArray. So you should use a getter
get myFormArray()
{
return this.form.get('myArray') as FormArray
}
then you can ask about myFormArray.at(i).valid. e.g.
add(index)
{
if (this.myFormArray.at(index).valid)
this.myFormArray.add(new FormGroup({...}))
else
(this.myFormArray.at(index) as FormGroup).markAsTouched()
}
NOTE: by defect a mat-error is showed when the FormControl is not valid and touched