【发布时间】:2021-07-06 19:44:08
【问题描述】:
仅在单击字段时才显示错误或显示错误,但现在在我当前的实现中,当调用 setRequiredVlidations 并且组件加载时,已显示所需的消息,如您在图像上看到的那样。它应该只在我点击输入字段或字段被触摸时显示。
用户在前端有选项,如果他选择选项 1,则验证器是请求如果他单击选项 2,则验证器不是请求。我的问题是在设置验证器时它已经在用户单击该字段之前显示,它应该只在用户触摸该字段时显示
if(option == 1) {
this.setRequiredVlidations();
}
initFormGroup() {
this.modelForm = this.formBuilder.group({
id: [this.model.id || 0],
emailAddress: [this.model.emailAddress, Validators.required],
firstName: this.model.firstName,
roleId: this.model.roleId,
lastName: this.model.lastName,
phoneNumber: this.model.phoneNumber,
companyName: this.model.companyName,
ssocredentials: [this.model.ssocredentials || ""],
accountId: this.accountId,
title: this.model.title,
isSso: [this.model.isSso || "",]
});
}
setRequiredVlidations() {
this.modelForm.get('firstName').setValidators(Validators.required)
this.modelForm.get('lastName').setValidators(Validators.required)
this.modelForm.get('companyName').setValidators(Validators.required)
}
#html
<mat-card-content>
<div fxLayout="row" fxLayoutAlign="space-between">
<div fxLayout="column" fxFlex="0 0 47%">
<mat-form-field class="full-width" appearance="fill" style="margin-top: 26px;">
<mat-label>First name</mat-label>
<input matInput formControlName="firstName">
<mat-error *ngIf="modelForm.get('firstName').hasError('required')" >
First name is required.
</mat-error>
</mat-form-field>
</div>
<div fxLayout="column" fxFlex="0 0 47%">
<mat-form-field class="full-width" appearance="fill" style="margin-top: 26px;">
<mat-label>Last name</mat-label>
<input matInput formControlName="lastName">
<mat-error *ngIf="modelForm.get('lastName').hasError('required') && isExisting === false">
Last name is required.
</mat-error>
</mat-form-field>
</div>
</div>
<div fxLayout="row" fxLayoutAlign="start">
<div fxLayout="column" fxFlex="0 0 47%">
<mat-form-field class="full-width" appearance="fill" style="margin-top:20px;">
<mat-label>Phone Number</mat-label>
<input matInput formControlName="phoneNumber">
</mat-form-field>
</div>
</div>
<div fxLayout="row" fxLayoutAlign="space-between">
<div fxLayout="column" fxFlex="0 0 47%">
<mat-form-field class="full-width" appearance="fill" style="margin-top: 20px;">
<mat-label>Company Name</mat-label>
<input matInput formControlName="companyName">
<mat-error *ngIf="modelForm.get('companyName').hasError('required') && isExisting === false">
Company Name is required.
</mat-error>
</mat-form-field>
</div>
<div fxLayout="column" fxFlex="0 0 47%">
<mat-form-field class="full-width" appearance="fill" style="margin-top: 17px;">
<mat-label>Title</mat-label>
<input matInput formControlName="title">
</mat-form-field>
</div>
</div>
</mat-card-content>
【问题讨论】:
标签: angular typescript angular-material