【问题标题】:How to show different validation messages for email validation in Angular2 using Validators class?如何使用 Validators 类在 Angular2 中为电子邮件验证显示不同的验证消息?
【发布时间】:2018-09-16 07:10:22
【问题描述】:

我正在使用 FormGroupFormBuilderValidators 类来验证 Angular2 应用程序中的表单。

这就是我定义电子邮件和密码验证所需的验证规则的方式:-

export class LoginComponent implements OnInit {
    loginFormGroup:FormGroup;
    adminLoginmodel = new Admin('', '', '', 'Emailsss','Passwordsss');  

    constructor(
       private route: ActivatedRoute,
       private router: Router,
       private _adminLogin: AdminLoginService,
       fb: FormBuilder
    ){
         this.loginFormGroup = fb.group({
            'email' : [null, Validators.compose([Validators.required, Validators.email])],
            'password': [null, Validators.required]
         });
    }
}

代码Validators.compose([Validators.required, Validators.email]) 会检查电子邮件字段是否为空以及提供的字符串是否为有效电子邮件。

但是,我不知道如何在不同的情况下显示不同的验证消息。说

  1. 如果电子邮件字段为空,我需要显示“请提供电子邮件 地址”
  2. 如果提供的电子邮件无效,那么我需要显示“提供的电子邮件不是有效的电子邮件”。

这是我在 html 中显示验证消息的方式:-

<div class="input-group input-group-lg" [ngClass]="{'has-error':!loginFormGroup.controls['email'].valid && loginFormGroup.controls['email'].touched}">
   <span class="input-group-addon"><i class="glyphicon glyphicon-user red"></i></span>
   <input type="text" class="form-control" placeholder="Email" id="email" name="email" [formControl]="loginFormGroup.controls['email']" [(ngModel)]="adminLoginmodel.email"/>
</div>
<div class="alert alert-danger" *ngIf="!loginFormGroup.controls['email'].valid">You must add an email.</div>

如何在不同的情况下显示不同的消息?

【问题讨论】:

    标签: angular validation angular2-forms angular2-formbuilder angular2-form-validation


    【解决方案1】:
    import { Component } from '@angular/core';
    import {FormControl, Validators} from '@angular/forms';
    
    @Component({
      selector: 'my-app',
      template: `
      <input [formControl]="ctrl" />
    
      <div *ngIf="ctrl.errors?.email">
        Provided email is not a valid email
      </div>
    
      <div *ngIf="ctrl.errors?.required">
        Please provide an email address
      </div>
      `,
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      ctrl = new FormControl(null, Validators.compose([Validators.email, Validators.required]));
    }
    

    Live demo

    在 cmets 中的讨论证明,这个具体问题的答案是:

    <div class="alert alert-danger" *ngIf="loginFormGroup.controls['email'].hasError('email')">Provide a valid email.</div>
    <div class="alert alert-danger" *ngIf="loginFormGroup.controls['email'].hasError('required')">Please provide an email address</div>
    

    【讨论】:

    • &lt;div *ngIf="ctrl.errors?.email"&gt; Provided email is not a valid email &lt;/div&gt;&lt;div *ngIf="ctrl.errors?.required"&gt; Please provide an email address &lt;/div&gt; 不显示任何消息。
    • 现场演示工作正常。在您的情况下,您需要将 *ngIf 表达式更改为 loginFormGroup.get('email').errors?.emailloginFormGroup.get('email').errors?.required
    • &lt;div class="alert alert-danger" *ngIf="loginFormGroup.get('email').errors?.required"&gt;You must add an email.&lt;/div&gt; 不显示任何错误信息
    • &lt;div class="alert alert-danger" *ngIf="loginFormGroup.controls['email'].hasError('email')"&gt;Provide a valid email.&lt;/div&gt; 可以解决问题。
    【解决方案2】:

    你可以用它来显示错误信息

    <div *ngIf="loginFormGroup.get('email').touched && loginFormGroup.get('email').hasError('required')">Please provide an email address</div>
      <div *ngIf="loginFormGroup.get('email').touched && loginFormGroup.get('email').hasError('email')">Provided email is not a valid email</div>
    

    【讨论】:

      猜你喜欢
      • 2017-07-11
      • 2018-09-16
      • 2021-03-26
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多