Disable Validators.Required Angular2

0

I have the following form using Validators.

 this.formulario = new FormGroup({
    CodigoTemp: new FormControl(null, [Validators.required]),
});

I need to disable Validators.Required if a checkbox is checked. Is there any way to do this?

I use Angular 2.

    
asked by anonymous 14.12.2017 / 18:28

1 answer

1

You can use the setValidators method as follows:

// Para utilizar uma validação
this.formulario.controls.CodigoTemp.setValidators([
    Validators.required,
]);

// Para remover as validações
this.formulario.controls.CodigoTemp.setValidators(null);

Remember that this method will replace all existing validations, in case you remove the validations, no problem. If you want to add a new validation, for example, maxLength , you should redefine the existing validations and the new validation you want, like this:

this.formulario.controls.CodigoTemp.setValidators([
    Validators.required,
    Validators.maxLength(50),
]);
  

As far as I could find, there is still no functionality to add validations, such as discussed here . p>

    
14.12.2017 / 18:40