What I'm trying to do is to add an invalid class to the parent div of each input based on if its valid or not.
In the rest of the form I have access to the input field control like so:
<div
class="form-group focus"
[ngClass]="!recipeForm.controls.name.valid ? 'invalid' : ''"
>
<input
type="text"
id="name"
class="form-control"
formControlName="name"
/>
<label for="name" class="shrink">Name</label>
</div>
[ngClass]="!recipeForm.controls.name.valid ? 'invalid' : ''"
But then I have a FormsArray of ingredients with name/amount controls.How do I have access to those?`
get controls() {
// a getter!
const FormArray = (<FormArray>this.recipeForm.get('ingredients')).controls;
for (let i = 0; i < FormArray.length; i++) {
console.log(FormArray[i]);
}
return FormArray;
}`
If I do FormArray[i].value.name it works but If I do FormArray[i].controls.name it says Property 'controls' does not exist on type 'AbstractControl' even though I can see that there is a controls property with name and amount.
My ts file:
onAddIngredient() {
(<FormArray>this.recipeForm.get('ingredients')).push(
new FormGroup({
name: new FormControl(null, Validators.required),
amount: new FormControl(null, [
Validators.required,
Validators.pattern(/^[1-9]+[0-9]*$/),
]),
})
);
}
HTML template:
<div class="form-ingredients" formArrayName="ingredients">
<div
class="form-amount-group"
*ngFor="let ingredientCtrl of controls; let i = index"
[formGroupName]="i"
>
<div class="form-group focus">
<input class="" type="text" id="name" class="form-control" />
<label for="name" class="shrink">Name</label>
</div>
<div class="form-group focus amount">
<input
type="number"
class="form-control"
formControlName="amount"
/>
<label for="name" class="shrink">Amount</label>
</div>
<div class="operations">
<div class="button plus">
<button><i class="fas fa-plus"></i></button>
</div>
<div class="button minus">
<button><i class="fas fa-minus"></i></button>
</div>
</div>
<button class="btn btn-sm" title="Delete Ingredient">
<i class="fa fa-trash"></i>
</button>
</div>
</div>