【发布时间】:2020-07-06 15:02:10
【问题描述】:
我创建了下面的指令来创建跨领域验证遵循英雄之旅示例https://angular.io/guide/form-validation
这是我的代码:
import { ValidatorFn, FormGroup, ValidationErrors } from "@angular/forms";
export const ConsentReceivedValidator: ValidatorFn = (control: FormGroup): ValidationErrors | null =>
{
const idNumber = control.get("idNumber");
const consent = control.get("consentConfirmed");
return idNumber && consent && idNumber.value !== null && consent.value === true ? {
"ConsentReceived": true } : null;
};
但是我得到以下错误:
Type '(control: FormGroup) => ValidationErrors | null' is not assignable to type 'ValidatorFn'.
这是在 html 中验证错误的方式:
<div>
<app-text-input-with-label labelText="ID Number" formControlItemName="idNumber"
[form]="contactInformationForm">
</app-text-input-with-label>
<app-checkbox-with-label [labelText]="getConsentText()" checkboxItemName="consentConfirmed"
[isContactInfo]="true" [form]="contactInformationForm">
</app-checkbox-with-label>
<div *ngIf="contactInformationForm.errors?.consentReceived"
class="alert alert-danger alert-sm text-left mt-1">
Please read and tick the box before proceeding.
</div>
<br>
<app-reference-input [form]="contactInformationForm"></app-reference-input>
</div>
</div>
【问题讨论】:
-
只需删除
: ValidatorFn即可。 -
这行得通,谢谢。
标签: html angular typescript angular-directive angular-validation