0

I'm building a form which on the top contains filters. I'll show the functions that are called when the error ocurrs

public onChangeSubType(requestSubType: any) {
 const typeId = this.filterFormGroup.get('requestType').value.value;
 const subTypeId = this.filterFormGroup.get('requestSubType').value.value;
 const accountId = this.filterFormGroup.get('account').value;

 if (!subTypeId) return;

 this.detailList = [];
 this.setRequestModel(requestSubType.type);

 this.fieldSettings = Utils.manageFields(typeId, subTypeId);
 this.buildInvoiceForm();

 if (subTypeId === 77) {
  this.invoiceFormGroup.patchValue({ terminatedEmployees: true });
  this.fetchTerminatedEmployees(accountId);
 } else {
  this.invoiceFormGroup.patchValue({ terminatedEmployees: false });
  this.fetchEmployees(accountId);
 }

}


private buildInvoiceForm() {
    if (this.invoiceFormGroup) this.invoiceFormGroup.reset();
    const requestType = this.filterFormGroup.get('requestType').value.name;
    const requestSubType =
      this.filterFormGroup.get('requestSubType').value.name;
    const mainAccount = this.filterFormGroup.get('mainAccount').value.name;
    const account = this.filterFormGroup.get('account').value.name;
    const defaultSubject = `${requestType} - ${requestSubType} - ${mainAccount} - ${account}`;
    this.extraData = {
      employees: this.employees,
      categories: this.requestCategories,
    };
    if (this.fieldSettings) {
      this.invoiceFormGroup = new FormGroup({
        category: new FormControl('', [Validators.required]),
        subject: new FormControl(defaultSubject, [
          Validators.required,
          Validators.maxLength(200),
        ]),
        selectAll: new FormControl(false),
        employee: new FormControl(null, [Validators.required]),
        description: new FormControl('', [
          Validators.required,
          Validators.maxLength(200),
        ]),
        activityDate: new FormControl(''),
        attachmentFiles: new FormControl(''),
        terminatedEmployees: new FormControl(false),
      });
  }

invoice.html template

<div class="invoice-request__field flex-basis-30">
            <span class="lssai-label font-weight-bold"
              >{{
                this.invoiceFormGroup.get('terminatedEmployees')?.value
                  ? 'Terminated'
                  : ''
              }}
              Employees*</span
            >
            <ng-multiselect-dropdown
              formControlName="employee"
              [placeholder]="'Select Employee'"
              [settings]="accountMultiSelectSettings"
              [data]="employees"
            >
            </ng-multiselect-dropdown>
          </div>

onChangeSubType is called whenever the subType changes and I get the following error

ERROR Error: There is no FormControl instance attached to form control element with name: 'employee'

filterValueChanges() is called onInit()

private filterValueChanges() {
    this.filterFormGroup.valueChanges.subscribe((form) => {
      if (form.requestType === null) {
        this.requestSubTypes = null;
      }
      if (form.requestSubType) {
        this.onChangeSubType(form.requestSubType);
      }
      this.checkForCategoryNotApplyValues();
    });
  }

Just a final comment. If I change formControlName="employee" for [formControl]="invoiceFormGroup.controls['employee']" it works but gives me a tslint error.

How can I solve the error so I can keep formControlName="employee".

5
  • Would be great if you could reproduce the issue on StackBlitz. Just a concern, the root form group for formControlName="employee" is really an invoiceFormGroup instance? Commented Nov 24, 2024 at 5:13
  • Does the dorpdown with formControlName directive have a parent with the directive: formGroupName="invoiceFormGroup"? Commented Nov 24, 2024 at 11:28
  • @YongShun I'll try to reproduce it. Yes it is. Commented Nov 24, 2024 at 18:36
  • @birodani Yes it does. Commented Nov 24, 2024 at 18:37
  • Really the ng-multiselect-dropdown should be enclosed between a form with <form [formGroup]="invoiceFormGroup">...</form>. Only if the "invoiceFormGroup" belong to another formGroup is when you use `<div formControlName="invoiceFormGroup" Commented Nov 25, 2024 at 7:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.