angular/aio/content/errors/NG1003.md
Dylan Hunn f6a096e3d4 fix(forms): Update a Forms validator error to use RuntimeError (#46537)
Replace `new Error()` in a forms Validators function with `RuntimeError`, for better tree-shakability. Also, improve the error messages, and add documentation.

PR Close #46537
2022-06-28 11:32:54 -07:00

877 B

@name Wrong Async Validator Return Type @category forms @shortDescription Async validator must return a Promise or Observable

@description Async validators must return a promise or an observable, and emit/resolve them whether the validation fails or succeeds. In particular, they must implement the AsyncValidatorFn API

export function isTenAsync(control: AbstractControl): 
  Observable<ValidationErrors> | null {
    const v: number = control.value;
    if (v !== 10) {
    // Emit an object with a validation error.
      return of({ 'notTen': true, 'requiredValue': 10 });
    }
    // Emit null, to indicate no error occurred.
    return of(null);
  }

@debugging Did you mistakenly use a synchronous validator instead of an async validator?

@reviewed 2022-06-28