Skip to content

bug(md-error/md-hint): It isn't being displayed below tag #5292

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

Closed
rafaelss95 opened this issue Jun 22, 2017 · 8 comments
Closed

bug(md-error/md-hint): It isn't being displayed below tag #5292

rafaelss95 opened this issue Jun 22, 2017 · 8 comments

Comments

@rafaelss95
Copy link
Contributor

rafaelss95 commented Jun 22, 2017

Bug, feature request, or proposal:

Bug.

What is the expected behavior?

md-error and md-hint should display below the tag when it's wrapped by another tag.

What is the current behavior?

If you wrap md-error or md-hint with any tag, it doesn't work as expected, it appears inside element itself.

<form [formGroup]="formGroup">
  <md-input-container>
    <input type="text" mdInput formControlName="num2">

    <!--can be any element here-->

    <div *ngIf="formGroup.get('num2').invalid"> // dummy condition to show the issue
      <md-error *ngIf="formGroup.hasError('required', 'num2')">
        Required
      </md-error>
      <md-error *ngIf="formGroup.hasError('pattern', 'num2')">
        Must be a number
      </md-error>
    </div>
  </md-input-container>
</form>

What are the steps to reproduce?

Providing a Plunker (or similar) is the best way to get the team to see your issue.
http://plnkr.co/edit/8QgsyvGx93l9YpucZZ0w?p=info

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

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

Angular: 4.2.4
Material: Material2#beta.7
OS: Linux
Typescript: 2.3.4
Browser: Only tested in Chrome

Is there anything else we should know?

@willshowell
Copy link
Contributor

See #5263 (comment)

@rafaelss95
Copy link
Contributor Author

@willshowell For me, it's really strange... even if I use it with ng-container that doesn't render any element (tag) it doesn't render in the right way.

@willshowell
Copy link
Contributor

As you can see here, md-error is selected with <ng-content select="md-error"> to be projected within mat-input-subscript-wrapper.

Unfortunately, ng-content select="" only selects top level components within md-input-container, and doesn't look within ng-container or ng-template...

I don't know the history of that behavior, but maybe worth filing an angular issue for.

@rafaelss95
Copy link
Contributor Author

@willshowell That's really a bad thing :(

Now I'm wondering if it's an issue or the expected behavior.

My real situation is the following:

I have a lot of inputs inside my components, like this:

<input type="text" mdInput [formControl]="nameCtrl">
<form-validation [control]="nameCtrl"></form-validation>

I have a reusable component named "form-validation" with this template:

<ng-container *ngIf="control.invalid && control.touched && control.errors | keys as errorKeys">
  <ng-container *ngIf="errorKeys[0] as firstError">
    <md-error [translate]="'errors.messages.' + firstError" 
              [translateParams]="control.getError(firstError)">
    </md-error>
  </ng-container>
</ng-container>

Since it doesn't seem possible to do this (to use md-error inside another elements), I'll have to go back to the ugly syntax by doing the same code twice:

<md-error [translate]="'errors.messages.' + control.invalid && control.touched && control.errors | keys | first"
          [translateParams]="control.getError(control.invalid && control.touched && control.errors | keys | first)">
</md-error>

@webUIdevelopment
Copy link

webUIdevelopment commented Jun 29, 2017

The angular material document says that "If an input element can have more than one error state, it is up to the consumer to toggle which messages should be displayed. This can be done with CSS, ngIf or ngSwitch."
If the md-error cannot be wrapped with in another element for eg div, how can I achieve the below structure?

<md-input-container>
<input mdInput required>
<div [ngSwitch]="switchCase">
  <md-error *ngSwitchCase="Case1">Error 1</md-error>
  <md-error *ngSwitchCase="Case2">Error 2</md-error>
  <md-error *ngSwitchCase="Case3">Error 3</md-error>
 </div>
</md-input-container>

@willshowell
Copy link
Contributor

@webUIdevelopment You may have to organize it like this:

<input mdInput required>
<md-error>{{ currentError(switchCase) }}</md-error>
currentError(val) {
  switch (val) {
    // ...
  }
}

...or this

As far as I can understand, support for dynamic content projection will help angular/angular#8563 once it's available

@jelbourn
Copy link
Member

jelbourn commented Jul 7, 2017

See #5263 (comment) as @willshowell said for an explanation

@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 6, 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

No branches or pull requests

4 participants