Reputation:
I am trying to validate radio buttons in angular 2 by temmplate driven but not working.I have searched in google also no one answered properly.Any genius can answer this question?
https://stackblitz.com/edit/angular-zpyekb?file=src/app/app.component.html
app.component.html
<form #f="ngForm">
<div *ngFor="let option of options">
<div class="radio">
<input type="radio"
name="radio"
id="radio-{{option.id}}"
[(ngModel)]="value"
[value]="option.value"/>
<label for="radio-{{option.id}}">{{option.id}}
</label>
</div>
</div>
<div *ngIf="f.submitted">Please select</div>
<button>Submit</button>
</form>
Upvotes: 1
Views: 529
Reputation: 169
<div *ngFor="let option of options">
<div class="radio">
<input type="radio"
name="radio"
id="radio-{{option.id}}"
[(ngModel)]="value"
[value]="option.value"
required
/>
<label for="radio-{{option.id}}">{{option.id}}
</label>
</div>
</div>
<div *ngIf="f.submitted && !f.valid">Please select</div>
May be you are looking for the above validation. If not can you please help to know what exactly you are looking for ?
Upvotes: 1