Validation in reactive Forms, Set SetValidators to not required

0

Good afternoon, I have a problem, I have burned all the ships and I can not get out of the mess, the issue is to change field validations in a reacive form based on the behavior of a group of fields. I am creating a recipe, it is a form with a header with several fields and a subgroup of fields that are repeated as rows,

cod barra | producto | cantidad | dias de vigencia |+| 

These are fields to fill out and in the first instance they are required. example

cod barra | producto | cantidad | dias de vigencia |+| (inputs para agregar datos)

0025 ------| amoxi..---|-----10-----|------ 5 --------------|-| 

(shows the newly loaded data and I can delete them by pressing -, all these only in the frond end, even without persisting in the database.)

I'm using formbuilder

  this.recetaform = this.fb.group({
  paciente: ['', [<any>Validators.required, <any>Validators.minLength(7), <any>Validators.maxLength(9)]],
  femision: [this.fechaHoy(), [<any>Validators.required]],
  servicio: ['', [<any>Validators.required]],
  idServicio: ['', [<any>Validators.required]],
  turno: ['', [<any>Validators.required]],
  prestador: ['', [<any>Validators.required]],
  idPrestador: ['', [<any>Validators.required]],
  diagnostico: ['', [<any>Validators.required]],
  idDiagnostico: ['', [<any>Validators.required]],
  productofgn: this.fb.group({
    cb: [''],
    producto: ['', [<any>Validators.required]],
    lote: ['', [<any>Validators.required]],
    cant: ['', [<any>Validators.required]],
    dias: ['', [<any>Validators.required]],
  })
});

The idea is that when loading the first tuple (this is stored in an array of objects by push) change the required validator of the fields:

product, lot, cant and days

a not required. and that if I delete that created tuple, I will return the required values that I had initially.

I had thought about counting the elements in the object vector that stores this data, but I realized that I do not know where to control, if the change of the form is to say with

this.recetaform.valueChanges.subscribe(data => this.unafuncion());

or when I add or remove elements to the vector. I know that now my interest data, I can not relate to the changes.

I was watching SetValidators as this tutorial shows:

link

but this is based on the change of a field with Boolean content that performs the control and based on that changes the validations.

of this menera:

ngOnInit() {
this.rForm.get('validate').valueChanges.subscribe(

  (validate) => {

      if (validate == '1') {
          this.rForm.get('name').setValidators([Validators.required, Validators.minLength(3)]);
          this.titleAlert = 'You need to specify at least 3 characters';
      } else {
          this.rForm.get('name').setValidators(Validators.required);
      }
      this.rForm.get('name').updateValueAndValidity();

  });

}

some hand ?, I hope to be clear, it's my first posting in stackOverflow, and I see that it is not simple to trasnmit an idea without all the code. : (

    
asked by Pablo Cabrol 23.08.2017 в 20:53
source

0 answers