Can I call a function inside the Validators in a reactive form?

0

I made a method and would like to use it as Validators, in a reactive form, in Angular 2. Is it 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())
    ]
],
    
asked by anonymous 07.02.2018 / 13:13

1 answer

0

Yes. for example:

 descricao: [null, [ this.validarObrigatoriedade ] ]

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