Inject a service in a custom validator in Angular2

Kelem

I try to use a service in a custom validator to check if a username already exists.

import {Component} from 'angular2/core';
import {
    Control,
    ControlGroup,
    FormBuilder
} from "angular2/common";
import {CharacterService} from "./character-service";

@Component({
    selector: 'register-character-form',
    template: `
        <h2 class="ui header">A new adventurer is coming...</h2>
        <form (ngSubmit)="register()" [ngFormModel]="characterForm" class="ui form">
            <div class="field">
                <label>Nom</label>
                <input ngControl="name">
            </div>
            <button type="submit" class="ui button">Enter in the adventure</button>
        </form>
    `,
    providers: [CharacterService]
})
export class RegisterCharacterFormCmp {
    characterForm: ControlGroup;
    name: Control;

    constructor(private _characterService: CharacterService, fb: FormBuilder) {
        this.name = fb.control('', this.characterNameValidator);

        this.characterForm = fb.group({
            name: this.name
        });
    }

    register(): void {
        //TODO: To implement
    }

    // Not works, this binds the control
    characterNameValidator(control: Control) {
        return this._characterService.isCharacterNameAlreadyExists(control.value) ? {nameCharacterAlreadyExistsError: true} : null;
    }
}

It doesn't work. In the characterNameValidator, 'this' references the control and not my class. The service is undefined. How can I use my service in the validator ?

More globally, how can I pass arguments in a custom validator ?

SnareChops

You need to control what this means in your validation. You can do so with bind

this.name = fb.control('', this.characterNameValidator.bind(this));

Everything else should work as expected then.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Inject custom service into a custom Validator

From Dev

Access a service from a custom validator in Angular2

From Dev

Inject custom service in another service in Angular 2

From Dev

How to Inject a service into an async validator?

From Dev

Access service from Custom Validator in Angular

From Dev

Creating a custom validator as a service in Symfony2

From Dev

How to make a custom validator in Angular2

From Dev

Custom validator for uniqueness in Angular2

From Dev

Angular2 FormBuilder: Using 'this' in a custom validator

From Dev

Angular2 custom validator not called

From Dev

How to inject custom string into service constructor in Angular?

From Dev

Inject service into class (not component) Angular2

From Dev

Angular2 inject parent service into child

From Dev

How to inject a service into custom .ts file in Angular 2+

From Dev

Angular 2 How can I inject a custom provider into a service?

From Dev

Angular2 Inject service in another service creates 2 instances

From Dev

Custom Validator Angular 2

From Dev

Symfony 3 - how to inject the validator into a service?

From Dev

Inject @PersistenceContext in a custom validator Spring Hibernate

From Dev

Angular 5:Calling service method inside custom validator

From Dev

Angular2 how to unit test a custom validator directive?

From Dev

Angular2 how to pass a parameter into a custom form control validator?

From

How to implement Custom Async Validator in Angular2/4/5

From Dev

angular2 custom template validator has stale values

From Dev

Angular Can't Inject Custom Service when Unit Testing Directive

From Dev

Extending Angular2 Http doesn't inject service

From Dev

Why can't Angular2 inject my service?

From Dev

Angular2 service testing : inject a dependency with beforeEach

From Dev

Angular2 Injected custom service is undefined

Related Related

  1. 1

    Inject custom service into a custom Validator

  2. 2

    Access a service from a custom validator in Angular2

  3. 3

    Inject custom service in another service in Angular 2

  4. 4

    How to Inject a service into an async validator?

  5. 5

    Access service from Custom Validator in Angular

  6. 6

    Creating a custom validator as a service in Symfony2

  7. 7

    How to make a custom validator in Angular2

  8. 8

    Custom validator for uniqueness in Angular2

  9. 9

    Angular2 FormBuilder: Using 'this' in a custom validator

  10. 10

    Angular2 custom validator not called

  11. 11

    How to inject custom string into service constructor in Angular?

  12. 12

    Inject service into class (not component) Angular2

  13. 13

    Angular2 inject parent service into child

  14. 14

    How to inject a service into custom .ts file in Angular 2+

  15. 15

    Angular 2 How can I inject a custom provider into a service?

  16. 16

    Angular2 Inject service in another service creates 2 instances

  17. 17

    Custom Validator Angular 2

  18. 18

    Symfony 3 - how to inject the validator into a service?

  19. 19

    Inject @PersistenceContext in a custom validator Spring Hibernate

  20. 20

    Angular 5:Calling service method inside custom validator

  21. 21

    Angular2 how to unit test a custom validator directive?

  22. 22

    Angular2 how to pass a parameter into a custom form control validator?

  23. 23

    How to implement Custom Async Validator in Angular2/4/5

  24. 24

    angular2 custom template validator has stale values

  25. 25

    Angular Can't Inject Custom Service when Unit Testing Directive

  26. 26

    Extending Angular2 Http doesn't inject service

  27. 27

    Why can't Angular2 inject my service?

  28. 28

    Angular2 service testing : inject a dependency with beforeEach

  29. 29

    Angular2 Injected custom service is undefined

HotTag

Archive