Form control required depending on the selection of another angular form control

Asked

Viewed 109 times

1

I have the following mat-checkbox:

<mat-checkbox formControlName="formControlIdentificador" color="primary" class="correcaoClasseCheck" labelPosition="before"></mat-checkbox>

<input formControlName="formControlNomeIdentificador" matInput placeholder="Qual era o nome no identificador?">

The second input (formControlNomedentifier) must be required if the checkbox is selected. How can I do this?

This is my Builder form:

    this.secondFormGroup = this._formBuilder.group({

    formControlIdentificador: new FormControl('', [
    ]),

    formControlNomeIdentificador: new FormControl('', [
    ])

  });

1 answer

1


I would try one of these two ways: first:

<input formControlName="formControlNomeIdentificador" matInput placeholder="Qual era o nome no identificador?" [required]="secondFormGroup.get('formControlIdentificador').value">

Or:

<input formControlName="formControlNomeIdentificador" matInput placeholder="Qual era o nome no identificador?" [required]="!secondFormGroup.valid">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.