0
votes

I have a FormGroup composite for two FormGroups and I want to do set Validation dynamically but I can't access to component to set it

I tried this: this.formGroupFather.get('formGroupSon1').controls['componentA'].setValidators(XXXXX)

----------------------------------------- V2 --------------------------

I have a structure like this:

<form [formGroup] = "configuracion" (ngSubmit)="onSubmit()">
  <div class="row" id="ConfigFacturacion">
    <!--  COL 1 -->
    <div class="col px-4">
      <form formGroupName="configPT">
         <input type="text" formControlName="prubA">
      </form>
    </div>

    <!--  COL 2 -->
    <div class="col px-4">
      <form formGroupName="configVS">
         <input type="text" formControlName="prubB">
      </form>
    </div>
  </div>
</form>

Whit a simple form I can create dynamically validator using this: this.formSimple.controls['field'].setValidators([Validators.required]);

But If I try this in my compiste form: this.configuracion.get('configPT').controls['prubA'].setValidators([Validators.required]);

throw this error: core.js:15723 ERROR TypeError: Cannot read property 'setValidators' of undefined

1
Could you please add what the exact error is you are getting or even better create a stackblitz/jsfiddle showing the problem you are facing?Erbsenkoenig
Do you have a sample stackblitz with some minimal code to work with?SiddAjmera
I think this is what you're looking for ViewChildmilo
Also, are these two form groups a part of two different components? What's the Component Structure like?SiddAjmera
can you share some code ?malbarmavi

1 Answers

1
votes

Finally I get it with this:

this.configuracion.get('configPT').get('prubA').setValidators([Validators.required]);
this.configuracion.get('configPT').get('prubA').updateValueAndValidity();