FR: Create a component that adds validation and error messages to any form control
Bug, feature request, or proposal:
feature request
What is the current behavior?
can't use checkbox and radio inside form-field.
What is the use-case or motivation for changing an existing behavior?
form-field provide a common way to display field description (hints) and message errors and I would like to have the same behavior for checkbox and radio.
<mat-form-field class="example-medium-width">
<mat-select matInput name="cars" [(ngModel)]="vehicles.cars" placeholder="Cars" multiple #field_cars="ngModel">
<mat-option *ngFor="let car of cars" [value]="car">
{{ car }}
</mat-option>
</mat-select>
</mat-form-field>
The Select implementation here is pretty standard for template-driven forms and it works like a charm. Thanks to the "multiple" here, one can select multiple values and then cars are submitted nicely which are all part of one single variable. Easy to cycle thru, easy to paint. No maps to deal with, No code to manage indexes, none of that. You get your cars as an array! Just cannot get any better than that.
I hope that when the checkbox implementation is done, it would be done the same way so it will be as easy and as consolidated as the select implementation in my example. I should be able to group checkboxes under one group and handle that hassles free just like the current select box
How about add mat-slider to the list?
Very Ugly temporary hack that hurts
Wrap a mat-form-filed with always floatPlaceholder and add a matInput that it's display:none.
<mat-form-field floatPlaceholder="always" class="mat-form-field--no-underline">
<!-- the matInput with display:none makes the magic. It's ugly, I've warned you -->
<input matInput placeholder="Input" style="display: none" >
<mat-radio-group>
<mat-radio-button value="1">Option 1</mat-radio-button>
<mat-radio-button value="2">Option 2</mat-radio-button>
</mat-radio-group>
</mat-form-field>
Now, we just need a workaround to remove the mat-input-underline.
.mat-form-field--no-underline .mat-input-underline {
background-color: transparent;
}
Result

PS: Make sure you share your
formControlNamebetweenmat-radio-group/mat-checkboxand the ugly[matInput] {display:none}
Eq: http://plnkr.co/edit/NrihcY?p=preview
I was learning about the mat-selection-list and I've realized that it comes with a Label as [mat-subheader].
It turned out it works for checkboxes and radio group as well.
Try something like so:
RadioGroup
<mat-list>
<h3 mat-subheader>Label for RadioGroup</h3>
<mat-radio-group class="example-radio-group" [(ngModel)]="favoriteSeason">
<mat-radio-button class="example-radio-button" *ngFor="let season of seasons" [value]="season">
{{season}}
</mat-radio-button>
</mat-radio-group>
</mat-list>
Checkbox Group
<mat-selection-list #groceries>
<h3 mat-subheader>Groceries</h3>
<mat-list-option checkboxPosition="before" value="bananas">Bananas</mat-list-option>
<mat-list-option checkboxPosition="before" selected value="oranges">Oranges</mat-list-option>
<mat-list-option checkboxPosition="before" value="apples">Apples</mat-list-option>
<mat-list-option checkboxPosition="before" value="strawberries">Strawberries</mat-list-option>
</mat-selection-list>
Eq: https://plnkr.co/edit/k73FSy?p=preview
@tinayuangao I don't think we want to have radio buttons or checkboxes inside of form fields. form fields are only intended for things that have the floating label & underline style that input and select share. But error messages on radio & checkboxes is a reasonable request
Perhaps what is missing is a label that could be used as a field description for fields, such as radio-group, checkbox list, slider and some others
I guess we can always use Material Typography such as .mat-h4 or just wrap the <h4> in a .mat-typography:
<section class="mat-typography">
<h4>Label for RadioGroup</h4>
<mat-radio-group class="example-radio-group" [(ngModel)]="favoriteSeason">
<mat-radio-button class="example-radio-button" *ngFor="let season of seasons" [value]="season">
{{season}}
</mat-radio-button>
</mat-radio-group>
</section>
Also mat-selection-list should be wrapable in mat-form-field too, for a common style of validation messages, hints, etc.
I've just created a new issue (https://github.com/angular/material2/issues/11333) presenting the expected behavior in another way: I just asked to support validation errors on such inputs.
Just wondering how this hasn't been implemented.
This must be a must for a lot of people. (Pun intended)
I have the same problem. For me I need the error message, when it is required and nothing was checked, inside a mat-readio-group. And the required indicator * is missing. It is set dynamically. Sure, I can add it again with ngIf and a new tag but it radio buttons and checkboxes are form fields, since the beginning. Input, textarea, checkbox, radio buttons and smth more.
all controls should work inside form-field. now I have to do extra work to make all the fields look consistently (label hint ...) and I need to remember if use mat-raido-group or checkbox then I need use my-form-field instead of mat-form-field that is very annoying
Still no solution with this problem? Tried today to put checkbox of form (reactive approach) and had to add custom styles to make it fit vertically right because impossible to put checkbox inside mat-form-field. Also mat-error is dispayed not correctly (probably cuz it is not inside mat-form-field afterall).
wrapping them in form-field would do the job, that's what I did for ngx-formly integration: https://formly.dev/ui/material
wrapping them in
form-fieldwould do the job, that's what I did forngx-formlyintegration: https://formly-js.github.io/ngx-formly/ui/material
Sure, but you provide your component as MatFormFieldControl and implement MatFormFieldControl<T>.
import { MatFormField, MatFormFieldControl } from '@angular/material';
@Component({
// (...)
providers: [{ provide: MatFormFieldControl, useExisting: FormlyWrapperFormField }],
})
export class FormlyWrapperFormField implements MatFormFieldControl<any> {
// (...)
}
Since Angular Material version 7 there is a guide for doing this with custom controls: Creating a custom form field control
We should be able to add this feature in the same way for the Angular Material radio buttons and checkboxes.
We should also look into how the Angular Material select component implements this.
Any update on a fix for this? This has been a recurring issue for many for quite a long time now...
I was having troubles hiding the underline. Eventually worked using ::ng-deep
::ng-deep .mat-form-field--no-underline .mat-form-field-underline {
background-color: transparent !important;
}
I'd like to add button toggle groups to this request, they basically behave the same as radio groups with different styling.
Can we know what is the point behind not supporting checkbox and radio in mat-form-field?
Having issue tho with Radio Buttons. Set them up as required and after user submit the form without checking the radio buttons, he doesn't get any error message. Very weird thing ...