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

279
Visualizações
Property 'controls' does not exist on type 'AbstractControl?

I am trying to implement a ValidatorFn interface, however, I am getting an issue when trying to use the 'controls' property on a formGroup of type 'AbstractControl', the error is: "Property 'controls' does not exist on type 'AbstractControl'", I originally attempted to set formGroup as type 'FormGroup', however, this brought upon an array of errors. My ValidatorFn interface is as follows:

1. import { FormGroup, ValidatorFn, AbstractControl } from '@angular/forms';
2. export function requireCheckboxesToBeCheckedValidator(minRequired = 1): ValidatorFn {
3.   return function validate (formGroup: AbstractControl) {
4.     let checked = 0;
5. 
6.     Object.keys(formGroup.controls).forEach(key => {
7.       const control = formGroup.controls[key];
8. 
9.       if (control.value === true) {
10.         checked ++;
11.       }
12.     });
13. 
14.     if (checked < minRequired) {
15.       return {
16.         requireOneCheckboxToBeChecked: true,
17.       };
18.     }
19. 
20.     return null;
21.   };
22. }

The error occurs when using the controls property on lines 6 and 7. Is there a workaround to this when using a formGroup of type AbstractControl?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can add a type guard to make sure you have a FormGroup, and you can also handle the case when you have just a FormControl or a FormArray.

export function requireCheckboxesBeCheckedValidator(minRequired = 1): ValidatorFn {
  return (formControl: AbstractControl) => {
    let ctrls: AbstractControl[];
  
    if (formControl instanceof FormGroup) {
      ctrls = Object.values(formControl.controls);
    } else if (formControl instanceof FormArray) {
      ctrls = formControl.controls;
    } else {
      ctrls = [ctrls];
    }
            
    const checked = formControl.controls.filter(({ value }) => value === true);

    return checked < minRequired ? { requireOneCheckboxToBeChecked: true } : null;
  }
}
over 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