Angular 自定义验证器错误(return Promise 或 Observable 的预期验证器)
Angular Custom Validator Error (Expected validator to return Promise or Observable)
我为 phone 字段编写了一个自定义验证器并得到
错误
Expected validator to return Promise or Observable.
为简单起见,我只需要检查 phone 号码是否少于 10 个字符
html
<div class="form-group col-xs-3 col-md-3"
[ngClass]="{
'has-error':(ersaForm.get('phone').touched || ersaForm.get('phone').dirty ) &&
!ersaForm.get('phone').valid
}">
<label for="phoneId" class="control-label">Phone</label><br />
<p-inputMask mask="(999) 999-9999" formControlName="phone" inputStyleClass="form-control" [style]="{'width': '100%','height':'34px'}" id="phoneId" placeholder="Phone (required)"></p-inputMask>
<span class="help-block" *ngIf="(ersaForm.get('phone').touched || ersaForm.get('phone').dirty ) &&
ersaForm.get('phone').errors">
<span *ngIf="ersaForm.get('phone').errors.phonePBXCheck">
invalivd Phone Number.
</span>
</span>
</div>
TS
function phoneCheck(phone: string): ValidatorFn{
return (c: AbstractControl): { [key: string]: boolean } | null => {
var phoneVal = c.value;
phoneVal = phoneVal.replace('(', '');
phoneVal = phoneVal.replace(')', '');
phoneVal = phoneVal.replace('-', '');
phoneVal = phoneVal.replace('_', '');
phoneVal = phoneVal.replace(' ', '');
console.log('custom validation ' + phoneVal);
if (c.value != undefined && isNaN(c.value) || phoneVal.lenght<10) {
return { 'phonePBXCheck': true };
};
return null;
};
}
this.ersaForm = this._fb.group({
phone: ['', Validators.required, phoneCheck('')],
});
我错过了什么?
编辑:您只需将验证器包装在数组中即可。
this.ersaForm = this._fb.group({
phone: ['', [Validators.required, phoneCheck('')]],
});
此外,作为建议,您可以从验证器中删除这些行:
phoneVal = phoneVal.replace('(', '');
phoneVal = phoneVal.replace(')', '');
phoneVal = phoneVal.replace('-', '');
phoneVal = phoneVal.replace('_', '');
phoneVal = phoneVal.replace(' ', '');
并使用 p-inputMask
的 unmask
属性来保持您的模型值干净:
<p-inputMask mask="(999) 999-9999" formControlName="phone"
inputStyleClass="form-control"
[unmask]="true"
[style]="{'width': '100%','height':'34px'}" id="phoneId"
placeholder="Phone (required)">
</p-inputMask>
Update:玩了一会儿后我注意到 p-inputMask
不支持其他验证器,它只为您提供 required
属性,即使您的自定义验证器被调用,控件本身也将保持有效。
我为 phone 字段编写了一个自定义验证器并得到
错误
Expected validator to return Promise or Observable.
为简单起见,我只需要检查 phone 号码是否少于 10 个字符
html
<div class="form-group col-xs-3 col-md-3"
[ngClass]="{
'has-error':(ersaForm.get('phone').touched || ersaForm.get('phone').dirty ) &&
!ersaForm.get('phone').valid
}">
<label for="phoneId" class="control-label">Phone</label><br />
<p-inputMask mask="(999) 999-9999" formControlName="phone" inputStyleClass="form-control" [style]="{'width': '100%','height':'34px'}" id="phoneId" placeholder="Phone (required)"></p-inputMask>
<span class="help-block" *ngIf="(ersaForm.get('phone').touched || ersaForm.get('phone').dirty ) &&
ersaForm.get('phone').errors">
<span *ngIf="ersaForm.get('phone').errors.phonePBXCheck">
invalivd Phone Number.
</span>
</span>
</div>
TS
function phoneCheck(phone: string): ValidatorFn{
return (c: AbstractControl): { [key: string]: boolean } | null => {
var phoneVal = c.value;
phoneVal = phoneVal.replace('(', '');
phoneVal = phoneVal.replace(')', '');
phoneVal = phoneVal.replace('-', '');
phoneVal = phoneVal.replace('_', '');
phoneVal = phoneVal.replace(' ', '');
console.log('custom validation ' + phoneVal);
if (c.value != undefined && isNaN(c.value) || phoneVal.lenght<10) {
return { 'phonePBXCheck': true };
};
return null;
};
}
this.ersaForm = this._fb.group({
phone: ['', Validators.required, phoneCheck('')],
});
我错过了什么?
编辑:您只需将验证器包装在数组中即可。
this.ersaForm = this._fb.group({
phone: ['', [Validators.required, phoneCheck('')]],
});
此外,作为建议,您可以从验证器中删除这些行:
phoneVal = phoneVal.replace('(', '');
phoneVal = phoneVal.replace(')', '');
phoneVal = phoneVal.replace('-', '');
phoneVal = phoneVal.replace('_', '');
phoneVal = phoneVal.replace(' ', '');
并使用 p-inputMask
的 unmask
属性来保持您的模型值干净:
<p-inputMask mask="(999) 999-9999" formControlName="phone"
inputStyleClass="form-control"
[unmask]="true"
[style]="{'width': '100%','height':'34px'}" id="phoneId"
placeholder="Phone (required)">
</p-inputMask>
Update:玩了一会儿后我注意到 p-inputMask
不支持其他验证器,它只为您提供 required
属性,即使您的自定义验证器被调用,控件本身也将保持有效。