【问题标题】:Validators compose angular seems to be not working验证器撰写角度似乎不起作用
【发布时间】:2019-10-18 12:25:42
【问题描述】:

我正在尝试使用 2 个验证器来验证用户的输入,这些验证器的模式与 Validators.compose([]) 相结合 但是然后在HTML中我试图验证但它不起作用 代码如下:

组件.ts

...
searchForm: FormGroup;

  constructor(
    private data: DataSharingService,
    private router: Router
  ){ }

  ngOnInit() {
    this.createSearchForm();
    this.newMessage()
  }

  private createSearchForm(): void {
    this.searchForm = new FormGroup({
      emailFormControl: new FormControl('', Validators.compose([
        Validators.pattern['/^[a-zA-Z]{2}[0-9]{6}/'],
        Validators.pattern['/^[0-9]{10}/']
      ]))
    });
  }
...

component.html

<form class="mainContainer" [formGroup]="searchForm">
<mat-form-field class="searchfield">
  <input matInput placeholder="Type in tax payer ID or initials " 
        formControlName="emailFormControl"
        i18n-placeholder="@@SearchPeHint">
  <mat-error *ngIf="searchForm.controls['emailFormControl'].hasError('required')" i18n="@@SearchPeParamsRequired">
       Tax payer ID or initials are required!
  </mat-error>
</mat-form-field>

【问题讨论】:

    标签: html angular typescript angular-forms


    【解决方案1】:

    您不必使用Validators.compose([])。在 mat-error 中检查 searchForm.controls['emailFormControl'].hasError('required')" 但 emailFormControl 没有必需的验证器。

    About Validators.compose()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-13
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多