1

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?

2
  • Could you please provide some code on stackblitz.com to reproduce your issue? Commented Feb 22, 2022 at 9:01
  • Please provide enough code so others can better understand or reproduce the problem. Commented Mar 6, 2022 at 7:38

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.