I have several input fields that need to be validated and it works fine with my custom made component error-msg when I assign local template variables (#requireSomeProperty1) and check them (*ngIf="requireSomeProperty1.errors")
<input type="hidden" [(ngModel)]="activeItem.SomeProperty1" name="SomeProperty1" #requireSomeProperty1="ngModel" [required]="someConditions1"/>
<error-msg *ngIf="requireSomeProperty1.errors" message="Property1 is required"></error-msg>
<input type="hidden" [(ngModel)]="activeItem.SomeProperty2" name="SomeProperty2" #requireSomeProperty2="ngModel" [required]="someConditions2"/>
<error-msg *ngIf="requireSomeProperty2.errors" message="Property2 is required"></error-msg>
Now instead of hard-coding them to html template I need to load them from database. All works fine, but I'm not sure how to achieve error checking / validating when they are generated with *ngFor.
I tried this, but I got parsing errors:
<div *ngFor="let item of itemsFromDB; index as i">
<input type="hidden" [(ngModel)]="activeItem[item.SomePropertyName]" [name]="item.SomePropertyName" #require_{{item.SomePropertyName}}="ngModel" [required]="item.SomeConditions">
<error-msg *ngIf="require_{{item.SomePropertyName}}.errors" [message]="item.ErrorMessage"></error-msg>
</div>
I also tried this, but I also got errors:
<div *ngFor="let item of itemsFromDB; index as i">
<input type="hidden" [(ngModel)]="activeItem[item.SomePropertyName]" [name]="item.SomePropertyName" #require_item.SomePropertyName="ngModel" [required]="item.SomeConditions">
<error-msg *ngIf="require_item.SomePropertyName.errors" [message]="item.ErrorMessage"></error-msg>
</div>
I also tried this and I didn't got any errors, but nothing was happening:
<div *ngFor="let item of itemsFromDB; index as i">
<input type="hidden" [(ngModel)]="activeItem[item.SomePropertyName]" [name]="item.SomePropertyName" #require+item.SomePropertyName="ngModel" [required]="item.SomeConditions">
<error-msg *ngIf="require+item.SomePropertyName.errors" [message]="item.ErrorMessage"></error-msg>
</div>
So, what am I missing? Or am I trying to do the impossible here?