表单验证

Improve overall data quality by validating user input for accuracy and completeness.

我们可以通过验证用户输入的准确性和完整性,来增强整体数据质量。

This cookbook shows how to validate user input in the UI and display useful validation messages using first the template-driven forms and then the reactive forms approach.

在本烹饪书中,我们展示在界面中如何验证用户输入,并显示有用的验证信息,先使用模板驱动表单方式,再使用响应式表单方式。

Read more about these choices in the Forms and the Reactive Forms guides.

参见表单响应式表单了解关于这些选择的更多知识。

Contents

目录

Try the live example to see and download the full cookbook source code.

查看在线例子,并下载整个烹饪书的源代码

在线例子

Simple template-driven forms

简单的模板驱动表单

In the template-driven approach, you arrange form elements in the component's template.

在模板驱动表单方法中,你在组件的模板中组织表单元素

You add Angular form directives (mostly directives beginning ng...) to help Angular construct a corresponding internal control model that implements form functionality. In template-drive forms, the control model is implicit in the template.

你可以添加Angular表单指令(通常为以ng开头的指令)来帮助Angular构建对应的内部控制模型,以实现表单功能。 控制模型在模板中是隐式的。

To validate user input, you add HTML validation attributes to the elements. Angular interprets those as well, adding validator functions to the control model.

要验证用户输入,你添加HTML验证属性到元素中。 Angular拦截这些元素,添加验证器函数到控制模型中。

Angular exposes information about the state of the controls including whether the user has "touched" the control or made changes and if the control values are valid.

Angular暴露关于控制状态的信息,包括用户是否已经“触摸“了控制器,或者用户已经作了更新和控制器的值是否还有效。

In this first template validation example, notice the HTML that reads the control state and updates the display appropriately. Here's an excerpt from the template HTML for a single input control bound to the hero name:

在第一个模板验证例子中,我们添加了更多HTML,来读取控制器状态并适当更新显示。 下面是模板HTML中提取的,一个绑定到英雄名字的输入框控制器:

template/hero-form-template1.component.html (Hero name)

<label for="name">Name</label> <input type="text" id="name" class="form-control" required minlength="4" maxlength="24" name="name" [(ngModel)]="hero.name" #name="ngModel" > <div *ngIf="name.errors && (name.dirty || name.touched)" class="alert alert-danger"> <div [hidden]="!name.errors.required"> Name is required </div> <div [hidden]="!name.errors.minlength"> Name must be at least 4 characters long. </div> <div [hidden]="!name.errors.maxlength"> Name cannot be more than 24 characters long. </div> </div>

Note the following:

请注意以下几点:

The full template repeats this kind of layout for each data entry control on the form.

整个模板为表单上的每种数据输入控制器重复这种布局。

Why check dirty and touched?

为何检查dirtytouched

The app shouldn't show errors for a new hero before the user has had a chance to edit the value. The checks for dirty and touched prevent premature display of errors.

当用户创建一个新英雄时,在还没有机会输入之前,我们不应该显示任何错误。 检查dirtytouched防止了这种过早的错误显示。

Learn about dirty and touched in the Forms guide.

参见表单章,学习关于dirtytouched的知识。

The component class manages the hero model used in the data binding as well as other code to support the view.

组件类管理用于数据绑定的英雄模型,它还有其他支持视图的代码。

template/hero-form-template1.component.ts (class)

export class HeroFormTemplate1Component { powers = ['Really Smart', 'Super Flexible', 'Weather Changer']; hero = new Hero(18, 'Dr. WhatIsHisWayTooLongName', this.powers[0], 'Dr. What'); submitted = false; onSubmit() { this.submitted = true; } addHero() { this.hero = new Hero(42, '', ''); } }

Use this template-driven validation technique when working with static forms with simple, standard validation rules.

在处理简单的、拥有标准验证规则的静态表单时,使用这种模板驱动验证方法。

Here are the complete files for the first version of HeroFormTemplateCompononent in the template-driven approach:

下面是第一个版本的使用模板驱动方法的HeroFormTemplateComponent

<div class="container"> <div [hidden]="submitted"> <h1>Hero Form 1 (Template)</h1> <form #heroForm="ngForm" *ngIf="active" (ngSubmit)="onSubmit()"> <div class="form-group"> <label for="name">Name</label> <input type="text" id="name" class="form-control" required minlength="4" maxlength="24" name="name" [(ngModel)]="hero.name" #name="ngModel" > <div *ngIf="name.errors && (name.dirty || name.touched)" class="alert alert-danger"> <div [hidden]="!name.errors.required"> Name is required </div> <div [hidden]="!name.errors.minlength"> Name must be at least 4 characters long. </div> <div [hidden]="!name.errors.maxlength"> Name cannot be more than 24 characters long. </div> </div> </div> <div class="form-group"> <label for="alterEgo">Alter Ego</label> <input type="text" id="alterEgo" class="form-control" name="alterEgo" [(ngModel)]="hero.alterEgo" > </div> <div class="form-group"> <label for="power">Hero Power</label> <select id="power" class="form-control" name="power" [(ngModel)]="hero.power" required #power="ngModel" > <option *ngFor="let p of powers" [value]="p">{{p}}</option> </select> <div *ngIf="power.errors && power.touched" class="alert alert-danger"> <div [hidden]="!power.errors.required">Power is required</div> </div> </div> <button type="submit" class="btn btn-default" [disabled]="!heroForm.form.valid">Submit</button> <button type="button" class="btn btn-default" (click)="addHero()">New Hero</button> </form> </div> <hero-submitted [hero]="hero" [(submitted)]="submitted"></hero-submitted> </div> import { Component } from '@angular/core'; import { Hero } from '../shared/hero'; @Component({ selector: 'hero-form-template1', templateUrl: './hero-form-template1.component.html' }) export class HeroFormTemplate1Component { powers = ['Really Smart', 'Super Flexible', 'Weather Changer']; hero = new Hero(18, 'Dr. WhatIsHisWayTooLongName', this.powers[0], 'Dr. What'); submitted = false; onSubmit() { this.submitted = true; } addHero() { this.hero = new Hero(42, '', ''); } }

Template-driven forms with validation messages in code

验证消息在代码中的模板驱动表单

While the layout is straightforward, there are obvious shortcomings with the way it's handling validation messages:

虽然布局很直观,但是我们处理验证消息的方法有明显的缺陷:

In this example, you can move the logic and the messages into the component with a few changes to the template and component.

只需要对模板和组件做出一些修改,我们可以将逻辑和消息移到组件中。

Here's the hero name again, excerpted from the revised template (Template 2), next to the original version:

下面也是关于英雄名字的控制器,从修改后的模板(“Template 2”)中抽取出来,与原来的版本相比:

<label for="name">Name</label> <input type="text" id="name" class="form-control" required minlength="4" maxlength="24" forbiddenName="bob" name="name" [(ngModel)]="hero.name" > <div *ngIf="formErrors.name" class="alert alert-danger"> {{ formErrors.name }} </div> <label for="name">Name</label> <input type="text" id="name" class="form-control" required minlength="4" maxlength="24" name="name" [(ngModel)]="hero.name" #name="ngModel" > <div *ngIf="name.errors && (name.dirty || name.touched)" class="alert alert-danger"> <div [hidden]="!name.errors.required"> Name is required </div> <div [hidden]="!name.errors.minlength"> Name must be at least 4 characters long. </div> <div [hidden]="!name.errors.maxlength"> Name cannot be more than 24 characters long. </div> </div>

The <input> element HTML is almost the same. There are noteworthy differences:

<input>元素的HTML几乎一样。但是下列有值得注意的区别:

Component class

组件类

The original component code for Template 1 stayed the same; however, Template 2 requires some changes in the component. This section covers the code necessary in Template 2's component class to acquire the Angular form control and compose error messages.

原组件代码的模板一没变化,只是模板二发生了变化。本节包括模板二的组件类,以获取Angular表单控制器和撰写错误信息。

The first step is to acquire the form control that Angular created from the template by querying for it.

第一步是获取Angular通过查询模板而生成的表单控制器。

Look back at the top of the component template at the #heroForm template variable in the <form> element:

回头看组件模板顶部,我们在<form>元素中设置#heroForm模板变量:

template/hero-form-template1.component.html (form tag)

<form #heroForm="ngForm" *ngIf="active" (ngSubmit)="onSubmit()">

The heroForm variable is a reference to the control model that Angular derived from the template. Tell Angular to inject that model into the component class's currentForm property using a @ViewChild query:

heroFrom变量是Angular从模板衍生出来的控制模型的引用。 我们利用@ViewChild来告诉Angular注入这个模型到组件类的currentForm属性:

template/hero-form-template2.component.ts (heroForm)

heroForm: NgForm; @ViewChild('heroForm') currentForm: NgForm; ngAfterViewChecked() { this.formChanged(); } formChanged() { if (this.currentForm === this.heroForm) { return; } this.heroForm = this.currentForm; if (this.heroForm) { this.heroForm.valueChanges .subscribe(data => this.onValueChanged(data)); } }

Some observations:

一些细节:

template/hero-form-template2.component.ts (handler)

onValueChanged(data?: any) { if (!this.heroForm) { return; } const form = this.heroForm.form; for (const field in this.formErrors) { // clear previous error message (if any) this.formErrors[field] = ''; const control = form.get(field); if (control && control.dirty && !control.valid) { const messages = this.validationMessages[field]; for (const key in control.errors) { this.formErrors[field] += messages[key] + ' '; } } } } formErrors = { 'name': '', 'power': '' };

The onValueChanged handler interprets user data entry. The data object passed into the handler contains the current element values. The handler ignores them. Instead, it iterates over the fields of the component's formErrors object.

onValueChanged处理器拦截用户数据输入。 包含当前元素值得data对象被传入处理器。 处理器忽略它们。相反,它迭代组件的formErrors对象。

The formErrors is a dictionary of the hero fields that have validation rules and their current error messages. Only two hero properties have validation rules, name and power. The messages are empty strings when the hero data are valid.

formErrors是一个词典,包含了拥有验证规则和当前错误消息的英雄控件。 只有两个英雄属性有验证规则,namepower。 当英雄数据有效时,这些消息的值为空字符串。

For each field, the onValueChanged handler does the following:

对于每个字段,这个onValueChanged处理器会做这些:

Next, the component needs some error messages of course—a set for each validated property with one message per validation rule:

很显然,我们需要一些错误消息,每个验证的属性都需要一套,每个验证规则需要一条消息:

template/hero-form-template2.component.ts (messages)

validationMessages = { 'name': { 'required': 'Name is required.', 'minlength': 'Name must be at least 4 characters long.', 'maxlength': 'Name cannot be more than 24 characters long.', 'forbiddenName': 'Someone named "Bob" cannot be a hero.' }, 'power': { 'required': 'Power is required.' } };

Now every time the user makes a change, the onValueChanged handler checks for validation errors and produces messages accordingly.

现在,每次用户作出变化时,onValueChanged处理器检查验证错误并按情况发出错误消息。

The benefits of messages in code

在代码中写消息的优点

Clearly the template got substantially smaller while the component code got substantially larger. It's not easy to see the benefit when there are just three fields and only two of them have validation rules.

很显然,模板变得小多了,组件代码变得大多了。当只有三个控件并且其中只有两个有验证规则时,我们很难看出好处。

Consider what happens as the number of validated fields and rules increases. In general, HTML is harder to read and maintain than code. The initial template was already large and threatening to get rapidly worse with the addition of more validation message <div> elements.

假设增加需要验证的控件和规则后会怎么样。 通常,HTML比代码更难阅读和维护。 初始的模板已经很大了,如果我们添加更多验证消息<div>,它会迅速变得更大。

After moving the validation messaging to the component, the template grows more slowly and proportionally. Each field has approximately the same number of lines no matter its number of validation rules. The component also grows proportionally, at the rate of one line per validated field and one line per validation message.

将验证消息移到组件后,模板的增长变得更加缓慢,幅度也小一些。 不管有多少个验证规则,每个控件的行数是差不多的。 组件也按比例增长,每增加一个控件增加一行,每个验证消息一行。

Both trends are manageable.

两条线容易维护。

Now that the messages are in code, you have more flexibility and can compose messages more efficiently. You can refactor the messages out of the component, perhaps to a service class that retrieves them from the server. In short, there are more opportunities to improve message handling now that text and logic have moved from template to code.

现在消息在代码中,我们有更多的灵活度。我们更加智能的撰写消息。 我们可以将消息重构出组件,比如到一个服务类,从服务端获取消息。 简而言之,有很多机会增强消息处理,因为文本和逻辑都已经从模板移到代码中。

FormModule and template-driven forms

FormModule 和模板驱动表单

Angular has two different forms modules—FormsModule and ReactiveFormsModule—that correspond with the two approaches to form development. Both modules come from the same @angular/forms library package.

Angular有两种不同的表单模块 - FormsModuleReactiveFormsModule - 它们与表单开发的两种方法对应。 两种模块都从同一个@angular/forms库。

You've been reviewing the "Template-driven" approach which requires the FormsModule. Here's how you imported it in the HeroFormTemplateModule.

我们一直在探讨模板驱动方法,它需要FormsModule。下面是如何在HeroFormTemplateModule中导入它:

template/hero-form-template.module.ts

import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { SharedModule } from '../shared/shared.module'; import { HeroFormTemplate1Component } from './hero-form-template1.component'; import { HeroFormTemplate2Component } from './hero-form-template2.component'; @NgModule({ imports: [ SharedModule, FormsModule ], declarations: [ HeroFormTemplate1Component, HeroFormTemplate2Component ], exports: [ HeroFormTemplate1Component, HeroFormTemplate2Component ] }) export class HeroFormTemplateModule { }

This guide hasn't talked about the SharedModule or its SubmittedComponent which appears at the bottom of every form template in this cookbook.

我们还没有讲SharedModule或者它的SubmittedComponent,它们出现在本烹饪书的每一个表单模板中。

They're not germane to the validation story. Look at the live example if you're interested.

它们与表单验证没有紧密的关系。如果你感兴趣,参见在线例子

Reactive forms with validation in code

在代码中验证响应式表单

In the template-driven approach, you markup the template with form elements, validation attributes, and ng... directives from the Angular FormsModule. At runtime, Angular interprets the template and derives its form control model.

在模板驱动方法中,你在模板中标出表单元素、验证属性和AngularFormsModule中的ng...指令。 在运行时间,Angular解释模板并从表单控制器模型衍生它。

Reactive Forms takes a different approach. You create the form control model in code. You write the template with form elements and form... directives from the Angular ReactiveFormsModule. At runtime, Angular binds the template elements to your control model based on your instructions.

响应式表单采用不同的方法。 你在代码中创建表单控制器模型,并用表单元素和来自Angular ReactiveFormsModule中的form...指令来编写模板。 在运行时间,Angular根据你的指示绑定模板元素到你的控制器模型。

This approach requires a bit more effort. You have to write the control model and manage it.

这个方法需要做一些额外的工作。你必须编写并管理控制器模型*

This allows you to do the following:

这可以让你:

The following cookbook sample re-writes the hero form in reactive forms style.

第三个烹饪书例子用响应式表单风格重新编写英雄表格。

Switch to the ReactiveFormsModule

切换到ReactiveFormsModule

The reactive forms classes and directives come from the Angular ReactiveFormsModule, not the FormsModule. The application module for the reactive forms feature in this sample looks like this:

响应式表单类和指令来自于Angular的ReactiveFormsModule,不是FormsModule。 本例中,应用模块的“响应式表单”特性是这样的:

src/app/reactive/hero-form-reactive.module.ts

import { NgModule } from '@angular/core'; import { ReactiveFormsModule } from '@angular/forms'; import { SharedModule } from '../shared/shared.module'; import { HeroFormReactiveComponent } from './hero-form-reactive.component'; @NgModule({ imports: [ SharedModule, ReactiveFormsModule ], declarations: [ HeroFormReactiveComponent ], exports: [ HeroFormReactiveComponent ] }) export class HeroFormReactiveModule { }

The reactive forms feature module and component are in the src/app/reactive folder. Focus on the HeroFormReactiveComponent there, starting with its template.

“响应式表单”特性模块和组件在app/reactive目录。 让我们关注那里的HeroFormReactiveComponent,先看它的模板。

Component template

组件模板

Begin by changing the <form> tag so that it binds the Angular formGroup directive in the template to the heroForm property in the component class. The heroForm is the control model that the component class builds and maintains.

我们先修改<form>标签,让Angular的formGroup指令绑定到组件类的heroForm属性。 heroForm是组件类创建和维护的控制器模型。

<form [formGroup]="heroForm" *ngIf="active" (ngSubmit)="onSubmit()">

Next, modify the template HTML elements to match the reactive forms style. Here is the "name" portion of the template again, revised for reactive forms and compared with the template-driven version:

接下来,我们修改模板HTML元素,来匹配响应式表单样式。 下面又是“name”部分的模板,响应式表单修改版本和模板驱动版本的比较:

<label for="name">Name</label> <input type="text" id="name" class="form-control" formControlName="name" required > <div *ngIf="formErrors.name" class="alert alert-danger"> {{ formErrors.name }} </div> <label for="name">Name</label> <input type="text" id="name" class="form-control" required minlength="4" maxlength="24" forbiddenName="bob" name="name" [(ngModel)]="hero.name" > <div *ngIf="formErrors.name" class="alert alert-danger"> {{ formErrors.name }} </div>

Key changes are:

关键变化:

A future version of reactive forms will add the required HTML validation attribute to the DOM element (and perhaps the aria-required attribute) when the control has the required validator function.

未来版本的响应式表单将会在控制器有required验证器函数时,添加required HTML验证属性到DOM元素(也可能添加aria-required属性)。

Until then, apply the required attribute and add the Validator.required function to the control model, as you'll see below.

在此之前,添加required属性以及添加Validator.required函数到控制器模型,像我们下面这样做:

The retreat from data binding is a principle of the reactive paradigm rather than a technical limitation.

不适用表单数据绑定是响应式模式的原则,而非技术限制。

Component class

组件类

The component class is now responsible for defining and managing the form control model.

组件类现在负责定义和管理表单控制器模型。

Angular no longer derives the control model from the template so you can no longer query for it. You can create the Angular form control model explicitly with the help of the FormBuilderclass.

Angular不再从模板衍生控制器模型,所以我们不能再查询它。 我们利用FormBuilder来显式创建Angular表单控制器模型。

Here's the section of code devoted to that process, paired with the template-driven code it replaces:

下面是负责该进程的代码部分,与被它取代的模板驱动代码相比:

heroForm: FormGroup; constructor(private fb: FormBuilder) { } ngOnInit(): void { this.buildForm(); } buildForm(): void { this.heroForm = this.fb.group({ 'name': [this.hero.name, [ Validators.required, Validators.minLength(4), Validators.maxLength(24), forbiddenNameValidator(/bob/i) ] ], 'alterEgo': [this.hero.alterEgo], 'power': [this.hero.power, Validators.required] }); this.heroForm.valueChanges .subscribe(data => this.onValueChanged(data)); this.onValueChanged(); // (re)set validation messages now } heroForm: NgForm; @ViewChild('heroForm') currentForm: NgForm; ngAfterViewChecked() { this.formChanged(); } formChanged() { if (this.currentForm === this.heroForm) { return; } this.heroForm = this.currentForm; if (this.heroForm) { this.heroForm.valueChanges .subscribe(data => this.onValueChanged(data)); } }

A real app would retrieve the hero asynchronously from a data service, a task best performed in the ngOnInit hook.

真实的应用很可能从数据服务异步获取英雄,这个任务最好在ngOnInit生命周期钩子中进行。

FormBuilder declaration

FormBuilder声明

The FormBuilder declaration object specifies the three controls of the sample's hero form.

FormBuilder声明对象指定了本例英雄表单的三个控制器。

Each control spec is a control name with an array value. The first array element is the current value of the corresponding hero field. The optional second value is a validator function or an array of validator functions.

每个控制器的设置都是控制器名字和数组值。 第一个数组元素是英雄控件对应的当前值。 第二个值(可选)是验证器函数或者验证器函数数组。

Most of the validator functions are stock validators provided by Angular as static methods of the Validators class. Angular has stock validators that correspond to the standard HTML validation attributes.

大多数验证器函数是Angular以Validators类的静态方法的形式提供的原装验证器。 Angular有一些原装验证器,与标准HTML验证属性一一对应。

The forbiddenNames validator on the "name" control is a custom validator, discussed in a separate section below.

"name"控制器上的forbiddenNames验证器是自定义验证器,在下面单独的小结有所讨论。

Learn more about FormBuilder in the Introduction to FormBuilder section of Reactive Forms guide.

到[响应式表单]的FormBuilder介绍部分,学习更多关于FormBuilder的知识。

Committing hero value changes

提交英雄值的更新

In two-way data binding, the user's changes flow automatically from the controls back to the data model properties. Reactive forms do not use data binding to update data model properties. The developer decides when and how to update the data model from control values.

在双向数据绑定时,用户的修改自动从控制器流向数据模型属性。 响应式表单不适用数据绑定来更新数据模型属性。 开发者决定何时如何从控制器的值更新数据模型。

This sample updates the model twice:

本例更新模型两次:

  1. When the user submits the form.

    当用户提交标单时

  2. When the user adds a new hero.

    当用户添加新英雄时

The onSubmit() method simply replaces the hero object with the combined values of the form:

onSubmit()方法直接使用表单的值得合集来替换hero对象:

onSubmit() { this.submitted = true; this.hero = this.heroForm.value; }

This example is lucky in that the heroForm.value properties just happen to correspond exactly to the hero data object properties.

本例非常“幸运”,因为heroForm.value属性正好与英雄数据对象属性对应。

The addHero() method discards pending changes and creates a brand new hero model object.

addHero()方法放弃未处理的变化,并创建一个崭新的hero模型对象。

addHero() { this.hero = new Hero(42, '', ''); this.buildForm(); }

Then it calls buildForm() again which replaces the previous heroForm control model with a new one. The <form> tag's [formGroup] binding refreshes the page with the new control model.

然后它再次调用buildForm,用一个新对象替换了之前的heroForm控制器模型。 <form>标签的[formGroup]绑定使用这个新的控制器模型更新页面。

Here's the complete reactive component file, compared to the two template-driven component files.

下面是完整的响应式表单的组件文件,与两个模板驱动组件文件对比:

import { Component, OnInit } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { Hero } from '../shared/hero'; import { forbiddenNameValidator } from '../shared/forbidden-name.directive'; @Component({ selector: 'hero-form-reactive3', templateUrl: './hero-form-reactive.component.html' }) export class HeroFormReactiveComponent implements OnInit { powers = ['Really Smart', 'Super Flexible', 'Weather Changer']; hero = new Hero(18, 'Dr. WhatIsHisName', this.powers[0], 'Dr. What'); submitted = false; onSubmit() { this.submitted = true; this.hero = this.heroForm.value; } } heroForm: FormGroup; constructor(private fb: FormBuilder) { } ngOnInit(): void { this.buildForm(); } buildForm(): void { this.heroForm = this.fb.group({ 'name': [this.hero.name, [ Validators.required, Validators.minLength(4), Validators.maxLength(24), forbiddenNameValidator(/bob/i) ] ], 'alterEgo': [this.hero.alterEgo], 'power': [this.hero.power, Validators.required] }); this.heroForm.valueChanges .subscribe(data => this.onValueChanged(data)); this.onValueChanged(); // (re)set validation messages now } onValueChanged(data?: any) { if (!this.heroForm) { return; } const form = this.heroForm; for (const field in this.formErrors) { // clear previous error message (if any) this.formErrors[field] = ''; const control = form.get(field); if (control && control.dirty && !control.valid) { const messages = this.validationMessages[field]; for (const key in control.errors) { this.formErrors[field] += messages[key] + ' '; } } } } formErrors = { 'name': '', 'power': '' }; validationMessages = { 'name': { 'required': 'Name is required.', 'minlength': 'Name must be at least 4 characters long.', 'maxlength': 'Name cannot be more than 24 characters long.', 'forbiddenName': 'Someone named "Bob" cannot be a hero.' }, 'power': { 'required': 'Power is required.' } }; } import { Component, AfterViewChecked, ViewChild } from '@angular/core'; import { NgForm } from '@angular/forms'; import { Hero } from '../shared/hero'; @Component({ selector: 'hero-form-template2', templateUrl: './hero-form-template2.component.html' }) export class HeroFormTemplate2Component implements AfterViewChecked { powers = ['Really Smart', 'Super Flexible', 'Weather Changer']; hero = new Hero(18, 'Dr. WhatIsHisWayTooLongName', this.powers[0], 'Dr. What'); submitted = false; onSubmit() { this.submitted = true; } addHero() { this.hero = new Hero(42, '', ''); } heroForm: NgForm; @ViewChild('heroForm') currentForm: NgForm; ngAfterViewChecked() { this.formChanged(); } formChanged() { if (this.currentForm === this.heroForm) { return; } this.heroForm = this.currentForm; if (this.heroForm) { this.heroForm.valueChanges .subscribe(data => this.onValueChanged(data)); } } onValueChanged(data?: any) { if (!this.heroForm) { return; } const form = this.heroForm.form; for (const field in this.formErrors) { // clear previous error message (if any) this.formErrors[field] = ''; const control = form.get(field); if (control && control.dirty && !control.valid) { const messages = this.validationMessages[field]; for (const key in control.errors) { this.formErrors[field] += messages[key] + ' '; } } } } formErrors = { 'name': '', 'power': '' }; validationMessages = { 'name': { 'required': 'Name is required.', 'minlength': 'Name must be at least 4 characters long.', 'maxlength': 'Name cannot be more than 24 characters long.', 'forbiddenName': 'Someone named "Bob" cannot be a hero.' }, 'power': { 'required': 'Power is required.' } }; } import { Component } from '@angular/core'; import { Hero } from '../shared/hero'; @Component({ selector: 'hero-form-template1', templateUrl: './hero-form-template1.component.html' }) export class HeroFormTemplate1Component { powers = ['Really Smart', 'Super Flexible', 'Weather Changer']; hero = new Hero(18, 'Dr. WhatIsHisWayTooLongName', this.powers[0], 'Dr. What'); submitted = false; onSubmit() { this.submitted = true; } addHero() { this.hero = new Hero(42, '', ''); } }

Run the live example to see how the reactive form behaves, and to compare all of the files in this cookbook sample.

运行在线例子,查看响应式表单是的行为,并与本章中的例子文件作比较。

Custom validation

自定义验证

This cookbook sample has a custom forbiddenNamevalidator() function that's applied to both the template-driven and the reactive form controls. It's in the src/app/shared folder and declared in the SharedModule.

本烹饪书例子有一个自定义forbiddenNameValidator函数,在模板驱动和响应式表单中都有使用。 它在app/shared目录,在SharedModule中被声明。

Here's the forbiddenNamevalidator() function:

下面是forbiddenNameValidator函数:

shared/forbidden-name.directive.ts (forbiddenNameValidator)

/** A hero's name can't match the given regular expression */ export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn { return (control: AbstractControl): {[key: string]: any} => { const name = control.value; const no = nameRe.test(name); return no ? {'forbiddenName': {name}} : null; }; }

The function is actually a factory that takes a regular expression to detect a specific forbidden name and returns a validator function.

该函数其实是一个工厂函数,接受一个正则表达式,用来检测指定的禁止的名字,并返回验证器函数。

In this sample, the forbidden name is "bob"; the validator rejects any hero name containing "bob". Elsewhere it could reject "alice" or any name that the configuring regular expression matches.

在本例中,禁止的名字是“bob”; 验证器拒绝任何带有“bob”的英雄名字。 在其他地方,只要配置的正则表达式可以匹配上,它可能拒绝“alice”或者任何其他名字。

The forbiddenNameValidator factory returns the configured validator function. That function takes an Angular control object and returns either null if the control value is valid or a validation error object. The validation error object typically has a property whose name is the validation key, 'forbiddenName', and whose value is an arbitrary dictionary of values that you could insert into an error message ({name}).

forbiddenNameValidator工厂函数返回配置好的验证器函数。 该函数接受一个Angular控制器对象,并在控制器值有效时返回null,或无效时返回验证错误对象。 验证错误对象通常有一个名为验证秘钥(forbiddenName)的属性。其值为一个任意词典,我们可以用来插入错误信息({name})。

Custom validation directive

自定义验证指令

In the reactive forms component, the 'name' control's validator function list has a forbiddenNameValidator at the bottom.

在响应式表单组件中,我们在'name'控制器的验证函数列表的底部添加了一个配置了的forbiddenNameValidator

reactive/hero-form-reactive.component.ts (name validators)

'name': [this.hero.name, [ Validators.required, Validators.minLength(4), Validators.maxLength(24), forbiddenNameValidator(/bob/i) ] ],

In the template-driven example, the <input> has the selector (forbiddenName) of a custom attribute directive, which rejects "bob".

在模板驱动组件的模板中,我们在name的输入框元素中添加了自定义属性指令的选择器(forbiddenName),并配置它来拒绝“bob”。

template/hero-form-template2.component.html (name input)

<input type="text" id="name" class="form-control" required minlength="4" maxlength="24" forbiddenName="bob" name="name" [(ngModel)]="hero.name" >

The corresponding ForbiddenValidatorDirective is a wrapper around the forbiddenNameValidator.

对应的ForbiddenValidatorDirective包装了forbiddenNamevalidator

Angular forms recognizes the directive's role in the validation process because the directive registers itself with the NG_VALIDATORS provider, a provider with an extensible collection of validation directives.

Angular表单接受指令在验证流程中的作用,因为指令注册自己到NG_VALIDATORS提供商中,该提供商拥有可扩展的验证指令集。

shared/forbidden-name.directive.ts (providers)

providers: [{provide: NG_VALIDATORS, useExisting: ForbiddenValidatorDirective, multi: true}]

Here is the rest of the directive to help you get an idea of how it all comes together:

指令的其它部分是为了帮你理解它们是如何合作的:

shared/forbidden-name.directive.ts (directive)

@Directive({ selector: '[forbiddenName]', providers: [{provide: NG_VALIDATORS, useExisting: ForbiddenValidatorDirective, multi: true}] }) export class ForbiddenValidatorDirective implements Validator, OnChanges { @Input() forbiddenName: string; private valFn = Validators.nullValidator; ngOnChanges(changes: SimpleChanges): void { const change = changes['forbiddenName']; if (change) { const val: string | RegExp = change.currentValue; const re = val instanceof RegExp ? val : new RegExp(val, 'i'); this.valFn = forbiddenNameValidator(re); } else { this.valFn = Validators.nullValidator; } } validate(control: AbstractControl): {[key: string]: any} { return this.valFn(control); } }

If you are familiar with Angular validations, you may have noticed that the custom validation directive is instantiated with useExisting rather than useClass. The registered validator must be this instance of the ForbiddenValidatorDirective—the instance in the form with its forbiddenName property bound to “bob". If you were to replace useExisting with useClass, then you’d be registering a new class instance, one that doesn’t have a forbiddenName.

To see this in action, run the example and then type “bob” in the name of Hero Form 2. Notice that you get a validation error. Now change from useExisting to useClass and try again. This time, when you type “bob”, there's no "bob" error message.

For more information on attaching behavior to elements, see Attribute Directives.

参见属性型指令章节。

Testing Considerations

测试时的注意事项

You can write isolated unit tests of validation and control logic in Reactive Forms.

我们可以为响应式表单的验证器和控制器逻辑编写孤立单元测试

Isolated unit tests probe the component class directly, independent of its interactions with its template, the DOM, other dependencies, or Angular itself.

孤立单元测试直接检测组件类,与组件和它的模板的交互、DOM、其他以来和Angular本省都无关。

Such tests have minimal setup, are quick to write, and easy to maintain. They do not require the Angular TestBed or asynchronous testing practices.

这样的测试具有简单设置#,快速编写和容易维护的特征。它们不需要Angular TestBed或异步测试工序。

That's not possible with template-driven forms. The template-driven approach relies on Angular to produce the control model and to derive validation rules from the HTML validation attributes. You must use the Angular TestBed to create component test instances, write asynchronous tests, and interact with the DOM.

这对模板驱动表单来说是不可能的。 模板驱动方法依靠Angular来生成控制器模型并从HTML验证属性中衍生验证规则。 你必须使用Angular TestBed来创建组件测试实例,编写异步测试并与DOM交互。

While not difficult, this takes more time, work and skill—factors that tend to diminish test code coverage and quality.

虽然这种测试并不困难,但是它需要更多时间、工作和能力 - 这些因素往往会降低测试代码覆盖率和测试质量。