I am working with Angular Material, I need to make a radio group required, so that the user has to select a radio button, before he can submit the form. So the form should be invalid while no radio button is selected.
this is the code
<form id="pipelineForm" name="pipelineForm" ng-submit="processForm()" flex layout="column" novalidate>
<md-radio-group ng-model="parameters.selected" ng-required="true" name="analyzerRG"
<md-radio-button ng-value="choiceObj" ng-repeat="choiceObj in parameters.choices" ng-required>
{{choiceObj.text}}
</md-radio-button>
</md-radio-group>
</form>
i have tried making individual <md-radio-button>
required, giving name
to the radio group and using ng-messages
for required
, but to no avail.
When i check the md-radio-group
in chrome element inspector, it always has the class="ng-valid ng-valid-required"
.
I can probably check the parameters.selected
property for validating the form on my own, but i would like if the correct classes are applied to the md-radio-group
and so the form is automatically invalid.
P.S. : There is a similar issue on Angular Material Github, but it seems to be closed now and the suggestions do not seem to work for me.
To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup . By grouping them together, the system ensures that only one radio button can be selected at a time.
Set Radio Button Selected in Angular It will set selected value of radio button in Angular's Reactive Forms. registrationForm = this. fb. group({ gender: ['male'] // Pass the name value in form control array. })
To validate radio button and checkbox, we need to use Validators from @angular/forms library. It is used while instantiating FormGroup . userForm = new FormGroup({ gender: new FormControl('', Validators. required), tc: new FormControl('', Validators.
<mat-radio-button> is used to select one option when we have multiple options. Approach: First, install the angular material using the above-mentioned command. After completing the installation, Import 'MatRadioModule' from '@angular/material/radio' in the app.
Check the solution on CodePen (https://codepen.io/jakobadam/pen/xqZoBR) as mentioned in the issue thread
https://github.com/angular/material/issues/1305#issuecomment-350047026
<md-input-container class="md-input-has-value"
ng-class="{ 'md-input-invalid' : form.fruit.$invalid && form.$submitted }">
<label class="md-required" translate>Fruit</label>
<md-radio-group md-autofocus name="fruit" ng-model="fruit" layout="row" required>
<md-radio-button value="Apple" class="md-primary">Apple</md-radio-button>
<md-radio-button value="Banana"> Banana </md-radio-button>
<md-radio-button value="Mango">Mango</md-radio-button>
</md-radio-group>
<div ng-messages="form.fruit.$error">
<div ng-message="required" translate>Yo. Select some fruit.</div>
</div>
</md-input-container>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With