Search code examples
angularvalidationcontrolvalueaccessor

Get validators type from CustomForm Component


I'm using Angular 7, I created a customForm Component (called app-lav-input), i.e. a component that inherits from ControlValueAccessor.

This is how I use app-lav-input:

HTML:

      <app-lav-input
        [label]="'name"
        formControlName="reference">
      </app-lav-input>

Component's code:

    this.basicDetailsForm = this.formBuilder.group({
      reference: ['', [ Validators.required ] ], ] ],
    });

I would like to add a '*' in app-lav-input template if Validators.required is used.

I could add another input to app-lav-input but it would be much nicer to get this information from the formControl.

How can I access this information from app-lav-input component's code ?


Solution

  • You could check if a form control has a required validator as shown here: Angular2: Find out if FormControl has required validator?