Can I call a function within Validators in a reactive form?

Asked

Viewed 53 times

0

I made a method and I’d like to use it as Validators, in a reactive form, in Angular 2. It is possible?

controle(){
    let data : number = this.formCep('nasc').value.split("-")[2];
    let atual = new Date().getFullYear();
    if(data - atual <= 17){
        alert('Você ainda não é maior de idade.')
        return false;
    }
    return true;
}


nasc: [null, [
    Validators.required,
    CustomValidators.minDate('1917-12-31'),
    CustomValidators.maxDate(new Date())
    ]
],

1 answer

0

yes. for example:

 descricao: [null, [ this.validarObrigatoriedade ] ]

 validarObrigatoriedade(input: FormControl) {
    return (input.value ? null : { obrigatoriedade: true });
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.