Search code examples
javascriptangularangular-formsangular-formbuilder

Angular validation for Non form controls


I want to validate a form using reactive form group for non form controls. I was using some custom components as part of my form. Please see the example

<form [formGroup]='formGroupName' >
  <input type='text' formControlName='control1'/>
  <!--The below component is part of some node module I cant do any changes in the below component -->
  <some-component [(value)]='model'></some-component>
</form>

 TypeScript
 this._fb.group({
   control1: ['', [Validators.required]],
   control2: ['', [Validators.required]]
 });

In the above code I want to bind control2 to some component model


Solution

  • I think that you can use

    <some-component [value]="formGroupName.get('control2').value"
                    (valueChange)="formGroupName.get('control2').setValue($event)"
    ></some-component>