Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

297
Views
¿Cómo puedo agregar/eliminar validadores condicionalmente en formularios reactivos?

Tengo una forma reactiva como esta.

 this.form = this.formBuilder.group({ ... email: [], receiveNewsletter: [], ... })

Quiero que todos los campos sean opcionales, excepto si el usuario receiveNewsletter , el campo de correo electrónico debe ser obligatorio. ¿Cómo puedo hacer eso?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe observar el valor de la casilla de verificación y agregar o eliminar el validador requerido según el valor de receiveNewsletter

 this.form.controls.receiveNewsletter.valueChanges.subscribe(value => { if(value){ this.form.controls.email.addValidators(Validator.required); } else { this.form.controls.email.removeValidators(Validator.required); } this.form.controls.email.updateValueAndValidity(); //remember to add this to update the form control state })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!