Skip to content

Instantly share code, notes, and snippets.

@0x14Rp
Forked from nicobytes/66-async-validations.md
Created April 29, 2020 21:52

Revisions

  1. @nicobytes nicobytes revised this gist Apr 12, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions 66-async-validations.md
    Original file line number Diff line number Diff line change
    @@ -47,4 +47,12 @@ validateEmail(control: AbstractControl) {
    })
    );
    }
    ```

    ### html

    ```html
    <p class="help is-danger" *ngIf="emailField.hasError('notAvailable')">
    Es correo esta ocupado
    </p>
    ```
  2. @nicobytes nicobytes revised this gist Apr 12, 2020. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions 66-async-validations.md
    Original file line number Diff line number Diff line change
    @@ -33,4 +33,18 @@ static validateEmail(userService: UserService) {
    );
    };
    }
    ```


    ```ts
    validateEmail(control: AbstractControl) {
    const value = control.value;
    return this.userService.checkEmail(value)
    .pipe(
    map(response => {
    const isEmailAvailable = response.isEmailAvailable;
    return isEmailAvailable ? null : {notAvailable: true};
    })
    );
    }
    ```
  3. @nicobytes nicobytes revised this gist Apr 12, 2020. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions 66-async-validations.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    ## service

    ```ts
    @Injectable({
    providedIn: 'root'
    @@ -14,4 +16,21 @@ export class UserService {
    .pipe(delay(500));
    }
    }
    ```

    ## validations

    ```ts
    static validateEmail(userService: UserService) {
    return (control: AbstractControl) => {
    const value = control.value;
    return userService.checkEmail(value)
    .pipe(
    map(response => {
    const isEmailAvailable = response.isEmailAvailable;
    return isEmailAvailable ? null : {notAvailable: true};
    })
    );
    };
    }
    ```
  4. @nicobytes nicobytes created this gist Apr 12, 2020.
    17 changes: 17 additions & 0 deletions 66-async-validations.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    ```ts
    @Injectable({
    providedIn: 'root'
    })
    export class UserService {

    constructor(
    private http: HttpClient
    ) { }

    checkEmail(email: string) {
    // simulate http.get()
    return of({ isEmailAvailable: email !== 'nicolas@gmail.com'})
    .pipe(delay(500));
    }
    }
    ```