【发布时间】:2021-01-14 10:44:23
【问题描述】:
我有两个输入(newPassword 和 confirmPassword),必须通过匹配来验证它们。
我有resetPasswordFormGroup,它在reset-password.component 中定义。
当我在 formgroup 中使用 MustMatch 自定义验证器时,我收到此错误。
实际上,我对如何使用自定义验证器感到困惑。
'{验证器类型的参数:(formGroup:FormGroup)=> void; }' 不可分配给类型为 'ValidatorFn | 的参数验证器Fn[] |抽象控制选项'。 对象字面量只能指定已知属性,而 'validator' 不存在于类型 'ValidatorFn |验证器Fn[] | AbstractControlOptions'.ts(2345)
resetPasswordFormGroup = new FormGroup({
currentPassword: new FormControl(null, Validators.required),
newPassword: new FormControl(null, Validators.required),
confirmNewPassword: new FormControl(null, Validators.required),
});
get currentPasswordFormControl() {
return this.resetPasswordFormGroup.get('currentPassword');
}
get newPasswordFormControl() {
return this.resetPasswordFormGroup.get('newPassword');
}
get confirmNewPasswordFormControl() {
return this.resetPasswordFormGroup.get('confirmNewPassword');
}
<form [formGroup]='resetPasswordFormGroup' (ngSubmit)="onPasswordChange()">
<mat-card-content>
<mat-form-field class="center-text">
<input matInput placeholder="please enter your current password" formControlName="currentPassword" />
<mat-error *ngIf="currentPasswordFormControl.hasError('required')">
<strong>current password</strong> is required
</mat-error>
</mat-form-field>
<mat-form-field class="center-text">
<input matInput placeholder="please enter your new password" formControlName="newPassword" />
<mat-error *ngIf="newPasswordFormControl.hasError('required')">
<strong> new password </strong> is required
</mat-error>
</mat-form-field>
<mat-form-field class="center-text">
<input matInput placeholder="please confirm your new password"
formControlName="confirmNewPassword" />
<mat-error *ngIf="confirmNewPasswordFormControl.hasError('required')">
<strong> confirm new password</strong> is required
</mat-error>
</mat-form-field>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="primary">
<div *ngIf="isSubmitting" class="loading">
<mat-spinner [diameter]="26" color="accent"></mat-spinner>
</div>
<span *ngIf="!isSubmitting">
submit
</span>
</button>
</mat-card-actions>
</form>
【问题讨论】:
标签: angular