components icon indicating copy to clipboard operation
components copied to clipboard

FR: Create a component that adds validation and error messages to any form control

Open aitboudad opened this issue 9 years ago • 52 comments

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.

aitboudad avatar Oct 18 '17 22:10 aitboudad

<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

halukkaramete avatar Oct 22 '17 21:10 halukkaramete

How about add mat-slider to the list?

leocaseiro avatar Nov 08 '17 01:11 leocaseiro

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

screen shot 2017-11-08 at 1 33 06 pm

PS: Make sure you share your formControlName between mat-radio-group/mat-checkbox and the ugly [matInput] {display:none}

Eq: http://plnkr.co/edit/NrihcY?p=preview

leocaseiro avatar Nov 08 '17 02:11 leocaseiro

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

leocaseiro avatar Nov 14 '17 04:11 leocaseiro

@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

mmalerba avatar Nov 14 '17 06:11 mmalerba

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 avatar Nov 14 '17 06:11 leocaseiro

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>

leocaseiro avatar Nov 16 '17 02:11 leocaseiro

Also mat-selection-list should be wrapable in mat-form-field too, for a common style of validation messages, hints, etc.

oktav777 avatar Jan 29 '18 16:01 oktav777

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.

anthony-o avatar May 15 '18 09:05 anthony-o

Just wondering how this hasn't been implemented.
This must be a must for a lot of people. (Pun intended)

ruant avatar Jun 23 '18 22:06 ruant

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.

Chris2011 avatar Jun 28 '18 15:06 Chris2011

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

hsinyu-chen avatar Jul 04 '18 11:07 hsinyu-chen

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).

AshurovRustam avatar Sep 05 '18 20:09 AshurovRustam

wrapping them in form-field would do the job, that's what I did for ngx-formly integration: https://formly.dev/ui/material

aitboudad avatar Sep 06 '18 17:09 aitboudad

wrapping them in form-field would do the job, that's what I did for ngx-formly integration: 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.

LayZeeDK avatar Nov 12 '18 12:11 LayZeeDK

Any update on a fix for this? This has been a recurring issue for many for quite a long time now...

JonesM87 avatar Nov 14 '18 15:11 JonesM87

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; }

danielnitu avatar Nov 22 '18 16:11 danielnitu

I'd like to add button toggle groups to this request, they basically behave the same as radio groups with different styling.

biltongza avatar Jan 17 '19 15:01 biltongza

Can we know what is the point behind not supporting checkbox and radio in mat-form-field?

ksuhiyp avatar Jan 20 '19 12:01 ksuhiyp

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 ...

MihailProcudin avatar Jan 29 '19 23:01 MihailProcudin