HTML

使用se和fromControl,共同实现定制化的表单验证(对于某些自定义的错误进行验证),方式如下:

  1. <div class="modal-header">
  2. <div class="modal-title">修改密码</div>
  3. </div>
  4. <div style="width: 100%; margin: 0 auto">
  5. <form nz-form se-container="1" #passwordForm="ngForm" [formGroup]="validateForm" [gutter]="2" [labelWidth]="110">
  6. <se label="原始密码" [error]="{ required: '请输入原始密码' }">
  7. <input type="text" nz-input formControlName="originPassword" placeholder="请输入原始密码" />
  8. </se>
  9. <se label="新密码" [error]="{ required: '未输入新密码', min: '密码位数不能少于8位' }">
  10. <input
  11. nz-input
  12. type="password"
  13. formControlName="newPassword"
  14. (ngModelChange)="updateConfirmValidator()"
  15. placeholder="请输入新密码,密码不少于8位"
  16. />
  17. </se>
  18. <se label="确认密码" [error]="{ required: '未输入确认密码', confirm: '两次输入的密码不一致' }">
  19. <input nz-input type="password" formControlName="confirmPassword" placeholder="请输入确认密码" />
  20. </se>
  21. </form>
  22. </div>
  23. <div class="modal-footer">
  24. <button nz-button type="button" (click)="close()">取消</button>
  25. <button nz-button nzType="primary" [disabled]="!validateForm.valid" (click)="savePassword()">确认</button>
  26. </div>

JS

  1. import { Component, OnInit, ViewChild } from '@angular/core';
  2. import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
  3. import { Department, DepartmentService, EmployeeService, SystemRole, SystemRoleService, User, UserService } from '@core';
  4. import { SEErrorRefresh } from '@delon/abc/se';
  5. import { SFSchema, SFUISchema, SFUploadWidgetSchema } from '@delon/form';
  6. import { _HttpClient } from '@delon/theme';
  7. import { CondOperator, RequestQueryBuilder } from '@nestjsx/crud-request';
  8. import { NzMessageService } from 'ng-zorro-antd/message';
  9. import { NzModalRef } from 'ng-zorro-antd/modal';
  10. import { from } from 'rxjs';
  11. import { map } from 'rxjs/operators';
  12. interface IFormData {
  13. originPassword: string;
  14. newPassword: string;
  15. confirmPassword: string;
  16. }
  17. @Component({
  18. selector: 'header-change-password',
  19. templateUrl: './edit.component.html',
  20. })
  21. export class HeaderChangePasswordComponent implements OnInit {
  22. validateForm: FormGroup;
  23. constructor(
  24. private modal: NzModalRef,
  25. public msgSrv: NzMessageService,
  26. public http: _HttpClient,
  27. private departmentService: DepartmentService,
  28. private systemRoleService: SystemRoleService,
  29. private employeeService: EmployeeService,
  30. private userService: UserService,
  31. private fb: FormBuilder,
  32. ) {
  33. this.validateForm = fb.group({
  34. originPassword: [null, [Validators.required]],
  35. newPassword: [null, [Validators.required, this.minLengthValidator]],
  36. confirmPassword: [null, [Validators.required, this.confirmationValidator]],
  37. });
  38. }
  39. updateConfirmValidator(): void {
  40. /** wait for refresh value */
  41. Promise.resolve().then(() => this.validateForm.controls.confirmPassword.updateValueAndValidity());
  42. }
  43. confirmationValidator = (control: FormControl): { [s: string]: boolean } => {
  44. if (!control.value) {
  45. return { required: true };
  46. } else if (control.value !== this.validateForm.controls.newPassword.value) {
  47. return { confirm: true, error: true };
  48. }
  49. return {};
  50. };
  51. minLengthValidator = (control: FormControl): { [s: string]: boolean } => {
  52. if (!control.value) {
  53. return { required: true };
  54. } else if (control.value.length < 8) {
  55. return { min: true, error: true };
  56. }
  57. return {};
  58. };
  59. ngOnInit(): void {}
  60. savePassword() {
  61. console.log(this.validateForm.value);
  62. }
  63. close() {
  64. this.modal.destroy();
  65. }
  66. }

关键点在于自定义的Validator,将输出的错误在se的[errorr]属性中表现出来。
image.png
image.png