Angular 自定义验证器错误(预期验证器返回 Promise 或 Observable)

rgo*_*oal 2 validation typescript primeng angular angular-reactive-forms

我为电话字段编写了一个自定义验证器并收到
错误

预期验证器返回 Promise 或 Observable。

为简单起见,我只需要检查电话号码是否少于 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>
Run Code Online (Sandbox Code Playgroud)

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('')],
        });
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

Dav*_*idZ 5

编辑:您只需要将验证器包装在一个数组中。

 this.ersaForm = this._fb.group({
   phone: ['', [Validators.required, phoneCheck('')]],
 });
Run Code Online (Sandbox Code Playgroud)

此外,作为一个建议,您可以从验证器中删除这些行:

phoneVal = phoneVal.replace('(', '');
phoneVal = phoneVal.replace(')', '');
phoneVal = phoneVal.replace('-', '');
phoneVal = phoneVal.replace('_', '');
phoneVal = phoneVal.replace(' ', '');
Run Code Online (Sandbox Code Playgroud)

而是使用unmaskof 属性p-inputMask来保持您的模型值干净:

 <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>
Run Code Online (Sandbox Code Playgroud)

更新:在玩了更多之后,我注意到它p-inputMask不支持其他验证器,它只required为您提供属性,即使调用了您的自定义验证器,控件本身也将保持有效。