Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mat-select doesn't show mat-error on submit #7634

Closed
sam-lex opened this issue Oct 8, 2017 · 3 comments · Fixed by #7640
Closed

mat-select doesn't show mat-error on submit #7634

sam-lex opened this issue Oct 8, 2017 · 3 comments · Fixed by #7640
Assignees

Comments

@sam-lex
Copy link

sam-lex commented Oct 8, 2017

Bug, feature request, or proposal:

Bug

What is the expected behavior?

Should show mat-error on submit (if invalid)

What is the current behavior?

mat-select becomes red as expected, but mat-error doesn't appear until clicked/touched.
doing control.markAsTouched(), control.markAsDirty() on submit, also doesn't work.

What are the steps to reproduce?

https://stackblitz.com/edit/angular-material2-issue-yxmtsq

  1. create mat-select + mat-error (I'm using required with ReactiveForms - Validators.required)
  2. add a submit button.
  3. Without touching the select, click on submit.
    select becomes red (invalid), but mat-error doesn't appear until you click/touch on the select.

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular: ^4.4.4,
"@angular/cdk": "^2.0.0-beta.12",
"@angular/material": "^2.0.0-beta.12",
"typescript": "^2.5.2",
OS: macOS Sierra
browser: Chrome

Is there anything else we should know?

Thanks :D

@sam-lex
Copy link
Author

sam-lex commented Oct 8, 2017

ok, I solved like this:

<mat-form-field>
  <mat-select placeholder="Fruits" #fruitSelect required [formControl]="fruit">
    <mat-option *ngFor="let f of fruits" [value]="f">
      {{f}}
    </mat-option>
  </mat-select>
  <mat-error *ngIf="fruitSelect.errorState && fruit.hasError('required')">
    <strong>required</strong>
  </mat-error>
</mat-form-field>

I added a variable #fruitSelect to mat-select,
then, on mat-error, I added a condition for fruitSelect.errorState.

I first tested with only fruitSelect.errorState, this worked. For me would've been ok, since I only have 1 validator. But what if I had multiple validators?
so then I tested with && fruit.hasError('required'), and it also worked. 🤔

I have no idea why only *ngIf="fruit.hasError('required')" doesn't.

@crisbeto crisbeto self-assigned this Oct 8, 2017
crisbeto added a commit to crisbeto/material2 that referenced this issue Oct 8, 2017
Fixes `mat-select` error messages not being shown when the parent form is submitted.

Fixes angular#7634.
@crisbeto crisbeto added the has pr label Oct 8, 2017
crisbeto added a commit to crisbeto/material2 that referenced this issue Oct 8, 2017
Fixes `mat-select` error messages not being shown when the parent form is submitted.

Fixes angular#7634.
@crisbeto
Copy link
Member

crisbeto commented Oct 8, 2017

It's definitely a bug. Until #7640 gets in you should be able to work around it by calling changeDetectorRef.markForCheck() when the form is submitted.

andrewseguin pushed a commit that referenced this issue Oct 9, 2017
Fixes `mat-select` error messages not being shown when the parent form is submitted.

Fixes #7634.
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants