You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
FR: Create a component that adds validation and error messages to any form control #7891
P5The team acknowledges the request but does not plan to address it, it remains open for discussionarea: material/form-fieldfeatureLabel used to distinguish feature request from other issues
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.
Reacted by Rémi Bleuse, Ben Elliott, Max Drljić, hanhehe, Leo Caseiro, Ravi Mehra, Sylvain Dumont, Tom Makin, Andreas Galazis, Edric Chan and 168 more
Reacted by Wellington Torrejais da Silva, jbellmont-phntm, Carlos Gomes Martinho and rrdhani14
<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
Reacted by Matt Walterspieler, Jochen Jonckheere, Vlad Poluch, Samet Alemdar, Chrystian Huot, Francesco Schwarz, dions00, Cristian Ban, Ville, Andrew Hunter and 22 more
Reacted by Nino Filiu, Francesco Schwarz, Christophe HOARAU, Ecliptic Rick and Jamie Wall
Wrap a mat-form-filed with always floatPlaceholder and add a matInput that it's display:none.
<mat-form-fieldfloatPlaceholder="always" class="mat-form-field--no-underline"><!-- the matInput with display:none makes the magic. It's ugly, I've warned you --><inputmatInputplaceholder="Input" style="display: none" ><mat-radio-group><mat-radio-buttonvalue="1">Option 1</mat-radio-button><mat-radio-buttonvalue="2">Option 2</mat-radio-button></mat-radio-group></mat-form-field>
Reacted by jsakamoto, padilla-jm, Martin Hettiger, robindijkhof, izogfif, Ricardo, buratinodev, Fernando, Jochen Jonckheere, Francesco Schwarz and 42 more
Reacted by Sabunkar Tejas Shailesh, Lars Gyrup Brink Nielsen, Ernestas Romeika, bulac, Christian Rondeau, joshcomley, Anand Rockzz, Daniel Kreider, sr480, Faraj Farook and 1 more
Reacted by Sura, Martin Hettiger, Sabunkar Tejas Shailesh, love-bees, bulac, joshcomley, Daniel Kreider, sr480 and keatkeat87
Reacted by Tim Potze, Matthew Leffler, Francesco Schwarz, Andrew Hunter, joshcomley, garg10may, Zeno França Junior, Mihaela Polanec, Aritrik Das, keatkeat87 and 1 more
Reacted by Jonathan Tougas, Mark Onyango, Sabunkar Tejas Shailesh, Nadya Djojosantoso, Daniel H. Huaman, joshcomley, sr480, Viveka Kannan and keatkeat87
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><h3mat-subheader>Label for RadioGroup</h3><mat-radio-groupclass="example-radio-group" [(ngModel)]="favoriteSeason"><mat-radio-buttonclass="example-radio-button" *ngFor="let season of seasons" [value]="season">
{{season}}
</mat-radio-button></mat-radio-group></mat-list>
@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
I guess we can always use Material Typography such as .mat-h4 or just wrap the <h4> in a .mat-typography:
<sectionclass="mat-typography"><h4>Label for RadioGroup</h4><mat-radio-groupclass="example-radio-group" [(ngModel)]="favoriteSeason"><mat-radio-buttonclass="example-radio-button" *ngFor="let season of seasons" [value]="season">
{{season}}
</mat-radio-button></mat-radio-group></section>
Any update on it. I use the alternative methods <div class="col-lg-6"> <div class="full-width"> <mat-checkbox formControlName="is_active">Utilisateur active</mat-checkbox> </div> </div>
Form control Name work on this case
Still a problem in 2021 - I think the general attitude is that "we know whats best for you" and so the workarounds continue.
We were able to make this work (for us at least) by adding in a custom label and relevant form errors below the checkbox.
<divclass="flex-column p-b-large"><labelfor="exampleCheck">
Checkbox example label:
<spanclass="required" aria-hidden="true"></span></label><mat-checkboxid="exampleCheck" formControlName="checked">
I Accept
</mat-checkbox><mat-error*ngIf="formErrors('checked')">
You must accept to continue.
</mat-error></div>
Also having same need, to add proper error messages to checkbox or radio groups.
@herman-rogers, your workaround is also just working partly. In that way the mat-error will not be correctly linked to the checkbox controls, so the error will not be correctly announced via screen readers for example. Normally the mat-form-field connects those and also adds all the necessary aria attributes, to make the error also accessible.
Here is my hacky contribution inspired by @leocaseiro. I needed validation. I did not need labels so see @leocaseiro example for that.
// This is all for a hacky fix to make radio buttons work with validation.form-field-radio-group {
&.mat-form-field-invalid.mat-radio-group{
color: red;
::ng-deep .mat-radio-outer-circle{
border-color: red;
}
}
.mat-input-element {
display: none
}
::ng-deep.mat-form-field-underline {
display: none
}
}
2021 and still the issue is around for the checkbox!!!
Reacted by Krishna Sahu, James Skinner, S, Rafael Lyra, Flo, Revati Mahajan, Rogerio Potenza, Bedini Fabio aka whisher , lesteral, Kurtis Jungersen and 1 more
I've implemented a Directive to emulate the MatFormAccessor for any NgControl, which you would use like this:
<mat-form-field><mat-label>Application Active</mat-label><mat-slide-toggleformControlName="active" workaroundMatFormAccessor>
Active
</mat-slide-toggle></mat-form-field>
Should work for other usecases but I didn't test it yet, maybe I will setup a small npm package for it.
If you're using an older version of Angular that doesn't implement hasValidator you can do this instead for the required() getter in WorkaroundMatFormAccessorDirective
get required(): boolean {
const control = this.ngControl.control;
if (!(control && control.validator)) {
return false;
}
const validator = control.validator({} as AbstractControl);
return validator && validator.required;
}
There are a couple reasons that we won't be able to commit to this work. The main reason is simply that supporting this is a lower priority than the current work of moving towards the latest Material Design spec. Second, there is no spec that we could use to implement errors for components other than input and select.
We would truly love to add this in (and many other highly requested features), but unfortunately we only have so many resources to work with.
P5The team acknowledges the request but does not plan to address it, it remains open for discussionarea: material/form-fieldfeatureLabel used to distinguish feature request from other issues
Activity
halukkaramete commentedon Oct 22, 2017
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
leocaseiro commentedon Nov 8, 2017
How about add
mat-sliderto the list?leocaseiro commentedon Nov 8, 2017
Very Ugly temporary hack that hurts
Wrap a
mat-form-filedwith alwaysfloatPlaceholderand add amatInputthat it'sdisplay:none.Now, we just need a workaround to remove the mat-input-underline.
Result
Eq: http://plnkr.co/edit/NrihcY?p=preview
leocaseiro commentedon Nov 14, 2017
I was learning about the
mat-selection-listand 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
Checkbox Group
Eq: https://plnkr.co/edit/k73FSy?p=preview
mmalerba commentedon Nov 14, 2017
@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
leocaseiro commentedon Nov 14, 2017
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
leocaseiro commentedon Nov 16, 2017
I guess we can always use Material Typography such as
.mat-h4or just wrap the<h4>in a.mat-typography:oktav777 commentedon Jan 29, 2018
Also
mat-selection-listshould be wrapable inmat-form-fieldtoo, for a common style of validation messages, hints, etc.anthony-o commentedon May 15, 2018
I've just created a new issue (#11333) presenting the expected behavior in another way: I just asked to support validation errors on such inputs.
ruant commentedon Jun 23, 2018
Just wondering how this hasn't been implemented.
This must be a must for a lot of people. (Pun intended)
35 remaining items
salem017 commentedon Nov 3, 2020
Any update on it. I use the alternative methods
<div class="col-lg-6"> <div class="full-width"> <mat-checkbox formControlName="is_active">Utilisateur active</mat-checkbox> </div> </div>Form control Name work on this case
herman-rogers commentedon Feb 24, 2021
Still a problem in 2021 - I think the general attitude is that "we know whats best for you" and so the workarounds continue.
We were able to make this work (for us at least) by adding in a custom label and relevant form errors below the checkbox.
cwuerzlhuber commentedon May 10, 2021
Also having same need, to add proper error messages to checkbox or radio groups.
@herman-rogers, your workaround is also just working partly. In that way the mat-error will not be correctly linked to the checkbox controls, so the error will not be correctly announced via screen readers for example. Normally the mat-form-field connects those and also adds all the necessary aria attributes, to make the error also accessible.
jcphlux commentedon Aug 16, 2021
Here is my hacky contribution inspired by @leocaseiro. I needed validation. I did not need labels so see @leocaseiro example for that.
rozhanroukhosh commentedon Sep 8, 2021
2021 and still the issue is around for the checkbox!!!
DanielHabenicht commentedon Jan 7, 2022
I've implemented a Directive to emulate the MatFormAccessor for any
NgControl, which you would use like this:Should work for other usecases but I didn't test it yet, maybe I will setup a small npm package for it.
Code:
https://gist.github.com/DanielHabenicht/b98beadb1f8e099f942053076f835bcc
palmtreefrb commentedon Feb 4, 2022
@DanielHabenicht
Thanks, this works for me with mat-checkbox.
wshaver commentedon Feb 7, 2022
Thanks @DanielHabenicht that saved me a bunch of time!
If you're using an older version of Angular that doesn't implement
hasValidatoryou can do this instead for therequired()getter inWorkaroundMatFormAccessorDirectivefrozenfrank commentedon Mar 17, 2023
It's now 2023. Any comment on why this feature is no longer under consideration? @andrewseguin
andrewseguin commentedon Mar 22, 2023
There are a couple reasons that we won't be able to commit to this work. The main reason is simply that supporting this is a lower priority than the current work of moving towards the latest Material Design spec. Second, there is no spec that we could use to implement errors for components other than input and select.
We would truly love to add this in (and many other highly requested features), but unfortunately we only have so many resources to work with.
meblum commentedon Feb 4, 2025
If you simply want the radio button to be red when invalid, you can do something like
rbalet commentedon Mar 24, 2025
If you want to avoid the
!importantflag, but also don't want it to be red on a disabled field ;)