Carlos
Carlos

Reputation: 11

How to set Validation in a field of FormGroup inside another FormGroup?

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

Upvotes: 0

Views: 493

Answers (1)

Carlos
Carlos

Reputation: 11

Finally I get it with this:

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

Upvotes: 1

Related Questions