angular8使用响应式表单(ng-zorro组件)

// 在所用到表单的模块中导入
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

我的表单组件文件

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-ledgers-statistics',
  templateUrl: './ledgers-statistics.component.html',
  styleUrls: ['./ledgers-statistics.component.scss']
})
export class LedgersStatisticsComponent implements OnInit {
  validateForm: FormGroup;
  // 自定义表单验证
  confirmValidator = (control: FormControl) => {
    if (!control.value) {
      return { error: true, required: true };
    } else if (control.value !== 'niuzai') {
      return { niuzai: true };
    }
    return {};
  }

  constructor(private fb: FormBuilder) {
    this.validateForm = this.fb.group({
      userName: ['', [Validators.required, Validators.maxLength(12), Validators.minLength(6)]],
      mobile: ['', [Validators.required, Validators.pattern(/^1(3|4|5|6|7|8|9)\d{9}$/)]],
      email: ['', [Validators.required, Validators.email]],
      password: ['', [this.confirmValidator]]
    });
  }

  ngOnInit() {
  }
  submit() {
    for (const key in this.validateForm.controls) {
      if (this.validateForm.controls.hasOwnProperty(key)) {
       this.validateForm.controls[key].markAsDirty();  // 将所有表单控件标记为已修改
       this.validateForm.controls[key].updateValueAndValidity();  // 重新计算表单控件的值和验证状态(校验表单)
        console.log(this.validateForm.controls[key]);
        // console.log(this.validateForm.controls[key].hasError('minlength'));
      }
    }
    console.log(this.validateForm.valid);
  }
}
<form nz-form  [formGroup]="validateForm">
  <nz-form-item>
    <nz-form-label [nzSpan]="5">自定义</nz-form-label>
    <nz-form-control nzHasFeedback [nzSpan]="12" [nzErrorTip]="combineTpl">
      <input nz-input formControlName="userName" required />
      <ng-template #combineTpl let-control>
        <ng-container *ngIf="control.hasError('maxlength')">MaxLength is 12</ng-container>
        <ng-container *ngIf="control.hasError('minlength')">MinLength is 6</ng-container>
        <ng-container *ngIf="control.hasError('required')">必填字段</ng-container>
      </ng-template>
    </nz-form-control>
  </nz-form-item>
  <nz-form-item>
    <nz-form-label [nzSpan]="5">手机</nz-form-label>
    <nz-form-control nzHasFeedback [nzSpan]="12" nzErrorTip="格式错误">
      <input nz-input formControlName="mobile" name="maxlength" />
    </nz-form-control>
  </nz-form-item>
  <nz-form-item>
    <nz-form-label [nzSpan]="5">邮箱</nz-form-label>
    <nz-form-control nzHasFeedback [nzSpan]="12" [nzErrorTip]="aa">
      <input nz-input formControlName="email" />
      <ng-template #aa let-control>
        <ng-container *ngIf="control.hasError('email')">邮箱格式错误</ng-container>
        <ng-container *ngIf="control.hasError('required')">必填字段</ng-container>
      </ng-template>
    </nz-form-control>
  </nz-form-item>
  <nz-form-item>
    <nz-form-label [nzSpan]="5">密码</nz-form-label>
    <nz-form-control nzHasFeedback [nzSpan]="12" [nzErrorTip]="name">
      <input nz-input formControlName="password" />
      <ng-template #name let-control>
        <ng-container *ngIf="control.hasError('niuzai')">不是牛仔</ng-container>
        <ng-container *ngIf="control.hasError('required')">必填字段</ng-container>
      </ng-template>
      
    </nz-form-control>
  </nz-form-item>
  <button (click)="submit()">提交</button>
</form>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值