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

127
Visualizações
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 à 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