Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

444
Vistas
Angular - Conditional Custom Validator on a FormGroup Inside of a FormGroup

I have a form that needs to check if one of a group of checkboxes is checked if a dropdown has a specific value. I have a validator that works on the checkboxes, but I only need it to check if the dropdown is a specific value. Below is the form and validation code. I've tried having the validator bind like this.requireCheckboxesValidator.bind(this), but then it just seems to ignore the validator

  readonly closeFormGroup: FormGroup;


  constructor(
    formBuilder: FormBuilder,
  ) {
    this.closeFormGroup = formBuilder.group({
      notes: [null],
      reason: [1, Validators.required],
      checkBoxGroup: new FormGroup({
        checkBox1: new FormControl(false),
        checkBox2: new FormControl(false),
        checkBox3: new FormControl(false),
        checkBox4: new FormControl(false),
        checkBox5: new FormControl(false),
      } ,this.requireCheckboxesValidator())
    });
  }
  public requireCheckboxesValidator(): ValidatorFn {
    return function validate(formGroup: FormGroup) {
      let checked = 0;
      console.log(formGroup);
      Object.keys(formGroup.controls).forEach(key => {
         const control = formGroup.controls[key];

         if (control.value === true) {
           checked++;
         }
       });

      if (checked < 1) {
        return {
          requireCheckboxesToBeChecked: true,
        };
      }

      return null;
    };
  }
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

I can suggest a different approach.
You can subscribe to valueChanges of your dropdown field and then if the specific value that is selected is what you expect, you can use addValidator to add the certain validator to your checkboxes form control.
I also like your idea of adding a custom validator (definitely the better approach) and it should actually work in that way as well. From what I understand you're trying to add the validator for the checkboxes only if the dropdown has a specific value ? In that case you need to change your logic to something like:

      Object.keys(formGroup.controls).forEach(key => {
         const control = formGroup.controls[key];

         if (control.value === true) {
           checked++;
         }
        let dropdownValue = undefined;
        if (key === 'dropdownFormControlKey') {
            dropdownValue = control.value;
        }
       });

      if (checked < 1) { // --- Add check to also see if the dropdown's value is what you expect and only then return the error
        return {
          requireCheckboxesToBeChecked: true,
        };
      }

      return null;
    };
  • Also, I would suggest not to define the validator function in your component, instead follow this article to see a proper approach - https://angular.io/guide/form-validation
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda