Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
Update Validators in FormControl

is there any way to update control after it declared, like

this.input = new FormControl('', Validators.required)
this.form = this.formBuilder.group({
  input = this.input
})

this.input.update('', Validators.maxlength(20))
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can use setValidators if you want to set new Validator(s) at a later point, you'd probably also want to update the value and validity, it can be run with updateValueAndValidity. Here's a simple example:

this.myForm.get('input').setValidators([Validators.required, 
                                          Validators.minLength(4)]);

this.myForm.get('input').updateValueAndValidity();

Demo

And if you want to update the field value, you can as mentioned use patchValue.

about 4 years ago · Santiago Trujillo Relatório

0

You can update a FormControl or FormGroup with the setValue method, or the patchValue method. In your case it is better to use setValue.

What patchValue does is if you want to update your form with some object, and that object contains more properties than the form (which means some properties do not exist on the form), with patchValue it will only get the values which exist on the form, and in this case if you use setValue, there will be an error. For more questions like this, its always best if you use the documentation (which has way more details than what I can explain here)

https://angular.io/docs/ts/latest/api/forms/index/FormControl-class.html

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda