表单验证
响应式表单验证
我们在 nz-form-control 上 提供了 nzValidateStatus``nzHasFeedback 等属性,当使用响应式表单时,可以自己定义校验的时机和内容。
nzValidateStatus: 校验状态,默认自动从nz-form-control中的NgControl获得校验状态,也可以手动指定为特定的NgControl。nzHasFeedback:用于给输入框添加反馈图标。nzSuccessTip``nzWarningTip``nzErrorTip``nzValidatingTip:设置不同状态校验文案。当同一种状态下存在多种提示情况时,
nzSuccessTip``nzWarningTip``nzErrorTip``nzValidatingTip均支持传入TemplateRef<{ $implicit: FormControl }类型,可以通过模板变量)导出FormControl后用于切换不同的提示信息。
<form nz-form [formGroup]="validateForm" (ngSubmit)="submitForm(validateForm.value)"><nz-form-item><nz-form-label [nzSpan]="7" nzRequired>Username</nz-form-label><nz-form-control [nzSpan]="12" nzHasFeedback nzValidatingTip="Validating..." [nzErrorTip]="userErrorTpl"><input nz-input formControlName="userName" placeholder="async validate try to write JasonWood" /><ng-template #userErrorTpl let-control><ng-container *ngIf="control.hasError('required')">Please input your username!</ng-container><ng-container *ngIf="control.hasError('duplicated')">The username is redundant!</ng-container></ng-template></nz-form-control></nz-form-item><nz-form-item><nz-form-label [nzSpan]="7" nzRequired>E-mail</nz-form-label><nz-form-control [nzSpan]="12" nzHasFeedback [nzErrorTip]="emailErrorTpl"><input nz-input formControlName="email" placeholder="email" type="email" /><ng-template #emailErrorTpl let-control><ng-container *ngIf="control.hasError('email')">The input is not valid E-mail!</ng-container><ng-container *ngIf="control.hasError('required')">Please input your E-mail!</ng-container></ng-template></nz-form-control></nz-form-item><nz-form-item><nz-form-label [nzSpan]="7" nzRequired>Password</nz-form-label><div><nz-form-control [nzSpan]="12" nzHasFeedback nzErrorTip="Please input your password!"><input nz-input type="password" formControlName="password" (ngModelChange)="validateConfirmPassword()" /></nz-form-control></div></nz-form-item><nz-form-item><nz-form-label [nzSpan]="7" nzRequired>Confirm Password</nz-form-label><nz-form-control [nzSpan]="12" nzHasFeedback [nzErrorTip]="passwordErrorTpl"><input nz-input type="password" formControlName="confirm" placeholder="confirm your password" /><ng-template #passwordErrorTpl let-control><ng-container *ngIf="control.hasError('required')">Please confirm your password!</ng-container><ng-container *ngIf="control.hasError('confirm')">Password is inconsistent!</ng-container></ng-template></nz-form-control></nz-form-item><nz-form-item><nz-form-label [nzSpan]="7" nzRequired>Comment</nz-form-label><nz-form-control [nzSpan]="12" nzErrorTip="Please write something here!"><textarea formControlName="comment" nz-input rows="2" placeholder="write any thing"></textarea></nz-form-control></nz-form-item><nz-form-item><nz-form-control [nzOffset]="7" [nzSpan]="12"><button nz-button nzType="primary" [disabled]="!validateForm.valid">Submit</button><button nz-button (click)="resetForm($event)">Reset</button></nz-form-control></nz-form-item></form>
import { Component } from '@angular/core';import { FormBuilder, FormControl, FormGroup, ValidationErrors, Validators } from '@angular/forms';import { Observable, Observer } from 'rxjs';@Component({selector: 'nz-demo-form-validate-reactive',templateUrl: `./form.html`,styles: [`[nz-form] {max-width: 600px;}button {margin-left: 8px;}`]})export class NzDemoFormValidateReactiveComponent {validateForm: FormGroup;submitForm(value: any): void {for (const key in this.validateForm.controls) {this.validateForm.controls[key].markAsDirty();this.validateForm.controls[key].updateValueAndValidity();}console.log(value);}resetForm(e: MouseEvent): void {e.preventDefault();this.validateForm.reset();for (const key in this.validateForm.controls) {this.validateForm.controls[key].markAsPristine();this.validateForm.controls[key].updateValueAndValidity();}}validateConfirmPassword(): void {setTimeout(() => this.validateForm.controls.confirm.updateValueAndValidity());}userNameAsyncValidator = (control: FormControl) =>new Observable((observer: Observer<ValidationErrors | null>) => {setTimeout(() => {if (control.value === 'JasonWood') {// you have to return `{error: true}` to mark it as an error eventobserver.next({ error: true, duplicated: true });} else {observer.next(null);}observer.complete();}, 1000);});confirmValidator = (control: FormControl): { [s: string]: boolean } => {if (!control.value) {return { error: true, required: true };} else if (control.value !== this.validateForm.controls.password.value) {return { confirm: true, error: true };}return {};};constructor(private fb: FormBuilder) {this.validateForm = this.fb.group({userName: ['', [Validators.required], [this.userNameAsyncValidator]],email: ['', [Validators.email, Validators.required]],password: ['', [Validators.required]],confirm: ['', [this.confirmValidator]],comment: ['', [Validators.required]]});}}
radio, checkbox响应式表单
目前没研究出用法,文档只有单个checkbox true/false
可能用模板驱动方式可以
参考:字段重构组件库的checkbox —— web/framework
formArray
这个例子不是很正确
Angular原生,官方文档关于radio,select的用法
Issue
