rgoal
rgoal

Reputation: 1316

Angular Custom Validator Error (Expected validator to return Promise or Observable)

I wrote a custom validator for the phone field and getting
error

Expected validator to return Promise or Observable.

for simplicity I just need to make check if the phone number i less than 10 characters

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('')],
        });

What AM I Missing?

Upvotes: 2

Views: 1996

Answers (1)

DavidZ
DavidZ

Reputation: 441

Edit: You just need to wrap your validators in an Array.

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

Also, for just as a suggestion you can remove these lines from your validator:

phoneVal = phoneVal.replace('(', '');
phoneVal = phoneVal.replace(')', '');
phoneVal = phoneVal.replace('-', '');
phoneVal = phoneVal.replace('_', '');
phoneVal = phoneVal.replace(' ', '');

and instead use the unmask attribute of p-inputMask to keep your model value clean:

 <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: After playing around a bit more I noticed that p-inputMask does not support other validators, it only provides the required attribute for you, even if your custom validator is called, the control itself will remain valid.

Upvotes: 5

Related Questions