Angular 2's documentation tells about the form validation. But only for input fields type text. My question is for Radio Buttons.
Here is HTML:
<input type="radio" required name='gender' value='Male' [(ngModel)]='gender'>Male
<input type="radio" required name='gender' value='Female' [(ngModel)]='gender'>Female
<button type='button' [disabled]="??">Next<button>
How to set disabled property to true if user hasn't selected any value.
With template driven forms you can expose your model to a local variable and query the errors object.
Since you didn't say which Forms Module (Reactive or Template driven) you were using here's a reactive forms version:
html
component code
Here's a Plunker: http://plnkr.co/edit/mWhYtc2nf8hSHFbLWlEx?p=preview
Validate mat-radio-group using Angular2
Not sure why the other answers are trying to attach variables to the radio elements, when the radios already become part of the
ngForm
therefore doing amyForm.valid
would work.Here's mine:
Just a
f.valid
should do.