简体   繁体   English

Angular Material 2 - 处理多个验证错误消息

[英]Angular Material 2 - handling multiple validation error messages

I am creating a form with Angular Material 2. I am using template driven form and I have email input which has two validators (required and email). 我正在使用Angular Material 2创建一个表单。我使用模板驱动的表单,我有电子邮件输入,有两个验证器(必需和电子邮件)。 In the doc for the input component ( https://material.angular.io/components/component/input ) it only says: 在输入组件的文档( https://material.angular.io/components/component/input )中,它只说:

"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." “如果输入元素可以有多个错误状态,则由消费者决定应该显示哪些消息。这可以通过CSS,ngIf或ngSwitch来完成。”

there is no example and I can't find it anywhere. 没有例子,我无法在任何地方找到它。

This is my html: 这是我的HTML:

...
<form (ngSubmit)="onSubmit(registrationForm)" #registrationForm="ngForm">
    ...

    <md-input-container floatPlaceholder="never">
      <input mdInput type="email" placeholder="Enter your email address" name="email" [(ngModel)]="email" required email>
      <md-error class="required">Email is required.</md-error>
      <md-error class="email">Invalid email.</md-error>
    </md-input-container>

    ...

Currently both messages are shown all the time. 目前,这两条消息始终显示。 Even if I enter some invalid email. 即使我输入了一些无效的电子邮件。

Any of the mentioned solutions (CSS, ngIf or ngSwitch) would be fine but I'd prefer CSS. 任何提到的解决方案(CSS,ngIf或ngSwitch)都可以,但我更喜欢CSS。

See example below. 见下面的例子。 A great way to get working examples is to review/search the Angular 2 Material GIT repo. 获得工作示例的一个好方法是查看/搜索Angular 2 Material GIT仓库。 Example below comes from https://github.com/angular/material2/blob/master/src/demo-app/input/input-demo.html 以下示例来自https://github.com/angular/material2/blob/master/src/demo-app/input/input-demo.html

      <md-input-container>
        <input mdInput placeholder="email" [formControl]="emailFormControl">
        <md-error *ngIf="emailFormControl.hasError('required')">
          This field is required
        </md-error>
        <md-error *ngIf="emailFormControl.hasError('pattern')">
          Please enter a valid email address
        </md-error>
      </md-input-container>

This is how i have implemented it in Angular 6 ( ReactiveFormsModule ) 这就是我在Angular 6中实现它的方法( ReactiveFormsModule

HTML HTML

<form [formGroup]="service.form" class="normal-form">
 <mat-grid-list cols="2" rowHeight="300px">
    <mat-grid-tile>
    <input type="hidden" formControlName="$key">
      <div class="controles-container">
          <mat-form-field>
            <input formControlName="mobile" matInput placeholder="Mobile*">
            <mat-error *ngIf="service.form.controls['mobile'].errors?.required">This field is mandatory.</mat-error>
            <mat-error *ngIf="service.form.controls['mobile'].errors?.minlength">Minimum 8 charactors needed.</mat-error>
        </mat-form-field>      
      </div>
    </mat-grid-tile>
</mat-grid-list>
</form> 

Component.ts Component.ts

export class MyComponent implements OnInit {

  constructor(public service :EmployeeService) { }

  ngOnInit() {
  }

  onClear() {
    this.service.form.reset();

  }
}

Service 服务

export class EmployeeService {

  constructor() { }

  form :FormGroup = new FormGroup({
    $key :new FormControl(null),
    mobile:new FormControl('',[Validators.required,Validators.minLength(10)]),  
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM