HTML
使用se和fromControl,共同实现定制化的表单验证(对于某些自定义的错误进行验证),方式如下:
<div class="modal-header">
<div class="modal-title">修改密码</div>
</div>
<div style="width: 100%; margin: 0 auto">
<form nz-form se-container="1" #passwordForm="ngForm" [formGroup]="validateForm" [gutter]="2" [labelWidth]="110">
<se label="原始密码" [error]="{ required: '请输入原始密码' }">
<input type="text" nz-input formControlName="originPassword" placeholder="请输入原始密码" />
</se>
<se label="新密码" [error]="{ required: '未输入新密码', min: '密码位数不能少于8位' }">
<input
nz-input
type="password"
formControlName="newPassword"
(ngModelChange)="updateConfirmValidator()"
placeholder="请输入新密码,密码不少于8位"
/>
</se>
<se label="确认密码" [error]="{ required: '未输入确认密码', confirm: '两次输入的密码不一致' }">
<input nz-input type="password" formControlName="confirmPassword" placeholder="请输入确认密码" />
</se>
</form>
</div>
<div class="modal-footer">
<button nz-button type="button" (click)="close()">取消</button>
<button nz-button nzType="primary" [disabled]="!validateForm.valid" (click)="savePassword()">确认</button>
</div>
JS
import { Component, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Department, DepartmentService, EmployeeService, SystemRole, SystemRoleService, User, UserService } from '@core';
import { SEErrorRefresh } from '@delon/abc/se';
import { SFSchema, SFUISchema, SFUploadWidgetSchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { CondOperator, RequestQueryBuilder } from '@nestjsx/crud-request';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { from } from 'rxjs';
import { map } from 'rxjs/operators';
interface IFormData {
originPassword: string;
newPassword: string;
confirmPassword: string;
}
@Component({
selector: 'header-change-password',
templateUrl: './edit.component.html',
})
export class HeaderChangePasswordComponent implements OnInit {
validateForm: FormGroup;
constructor(
private modal: NzModalRef,
public msgSrv: NzMessageService,
public http: _HttpClient,
private departmentService: DepartmentService,
private systemRoleService: SystemRoleService,
private employeeService: EmployeeService,
private userService: UserService,
private fb: FormBuilder,
) {
this.validateForm = fb.group({
originPassword: [null, [Validators.required]],
newPassword: [null, [Validators.required, this.minLengthValidator]],
confirmPassword: [null, [Validators.required, this.confirmationValidator]],
});
}
updateConfirmValidator(): void {
/** wait for refresh value */
Promise.resolve().then(() => this.validateForm.controls.confirmPassword.updateValueAndValidity());
}
confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
if (!control.value) {
return { required: true };
} else if (control.value !== this.validateForm.controls.newPassword.value) {
return { confirm: true, error: true };
}
return {};
};
minLengthValidator = (control: FormControl): { [s: string]: boolean } => {
if (!control.value) {
return { required: true };
} else if (control.value.length < 8) {
return { min: true, error: true };
}
return {};
};
ngOnInit(): void {}
savePassword() {
console.log(this.validateForm.value);
}
close() {
this.modal.destroy();
}
}
关键点在于自定义的Validator,将输出的错误在se的[errorr]属性中表现出来。