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

123
Vistas
How to apply custom validator on Form group control

I have Root FormGroup that contains different different FormGroup .I want to apply validator on control between two or more FormGroup .

For Example:

I have to two FormGroup mobile and workPhone and each FormGroup has different controls such as number etc.

Now i have to write validator on both mobile and workPhone to become one optional on enter value in number control. for more refrence please see the example on stackblitz.

how to achieve this Initial both control will be required. But when I enter the work phone number, the mobile number should become optional And Vice Versa.

appcomponent.ts

import { Component, OnInit } from '@angular/core';
import {
  AbstractControl,
  FormBuilder,
  FormControl,
  FormGroup,
  ValidationErrors,
  ValidatorFn,
  Validators,
} from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  rootForm!: FormGroup;
  constructor(private fb: FormBuilder) {}
  ngOnInit() {
    this.rootForm = this.fb.group(
      {
        mobile: new FormGroup({
          countryCode: new FormControl('+1'),
          number: new FormControl('', [Validators.required]),
        }),

        workPhone: new FormGroup({
          countryCode: new FormControl('+1'),
          number: new FormControl('', [Validators.required]),
        }),
      },
      { Validators: EitherMobileOrWorkPhoneRequired }
    );
  }

  public onSubmit(value) {
    console.log('Form Invalid:' + this.rootForm.invalid);
  }
}

export const EitherMobileOrWorkPhoneRequired: ValidatorFn = (
  control: AbstractControl
): ValidationErrors | null => {
  console.log(control);
  return null;
};

If any other details required. Please let me know. Thanks.

about 4 years ago · Juan Pablo Isaza
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