Skip to content

Angular 11: FormGroup Code Which Worked Using Angular 10 Now FailsΒ #40222

@mylifeandcode

Description

@mylifeandcode

🐞 bug report

Affected Package

The issue is caused by package @angular/angular (reactive_errors.ts)

Is this a regression?

Yes, the previous version in which this bug was not present was: Angular 10

Description

The code for one of my components fails with an error concerning the FormGroup. This code worked as-is under Angular 10, before upgrading to 11. I originally found this problem when running the unit tests for this component, and then replicated the issue live while running the app.

The error in question is: formGroup expects a FormGroup instance. Please pass one in.

πŸ”¬ Minimal Reproduction

I'm unable to share this code, but the component in question takes in an instance of FormBuilder as shown below:
constructor(private _formBuilder: FormBuilder, private _registrationService: RegistrationService) { }

Then, on ngOnInit(), it makes a call to a function which sets up the FormGroup:

async ngOnInit() {

    this.displayLoader = false;

    this.buildForm();

Here's the function which creates the FormGroup:

  private buildForm(): void {

    this.certEvalFilterForm = this._formBuilder.group({
      id: ['', [Validators.minLength(2) , Validators.pattern("^[0-9]*$") ]],
      firstName: ['', [Validators.minLength(2), this.nameValidator]],
      lastName: ['', [Validators.minLength(2), this.nameValidator]],
      examAdmin: ['', Validators.required],
      examName: '',
      issues: '',
      evalStatus: '',
      regDateFrom:  [ '', [ this.validateTodaysDate ,this.validateToFromDates('regDateFrom', 'regDateTo' ) ] ], 
      regDateTo: ['', [ this.validateTodaysDate , this.validateToFromDates('regDateFrom', 'regDateTo' ) ] ],
      attempt: '',
      isFinalEval: false
    });
  }

πŸ”₯ Exception or Error




ERROR Error: formGroup expects a FormGroup instance. Please pass one in.

       Example:

       
    
In your class: this.myGroup = new FormGroup({ firstName: new FormControl() }); at Function.missingFormException (reactive_errors.ts:38) at FormGroupDirective._checkFormPresent (form_group_directive.ts:300) at FormGroupDirective.ngOnChanges (form_group_directive.ts:98) at FormGroupDirective.rememberChangeHistoryAndInvokeOnChangesHook (core.js:1471) at callHook (core.js:2490) at callHooks (core.js:2456) at executeInitAndCheckHooks (core.js:2408) at selectIndexInternal (core.js:8181) at Module.Ι΅Ι΅advance (core.js:8164) at CertEvalFilterComponent_Template (cert-eval-filter.component.html:12) defaultErrorLogger @ core.js:5967 core.js:5967 ERROR TypeError: Cannot read property 'get' of undefined at FormGroupDirective.addControl (form_group_directive.ts:140) at FormControlName._setUpControl (form_control_name.ts:207) at FormControlName.ngOnChanges (form_control_name.ts:146) at FormControlName.rememberChangeHistoryAndInvokeOnChangesHook (core.js:1471) at callHook (core.js:2490) at callHooks (core.js:2456) at executeInitAndCheckHooks (core.js:2408) at selectIndexInternal (core.js:8181) at Module.Ι΅Ι΅advance (core.js:8164) at CertEvalFilterComponent_Template (cert-eval-filter.component.html:12)

🌍 Your Environment

Angular Version:




Angular CLI: 11.0.5
Node: 12.14.1
OS: win32 x64

Angular: 11.0.5
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1100.5
@angular-devkit/build-angular   0.1100.5
@angular-devkit/core            11.0.5
@angular-devkit/schematics      11.0.5
@angular/cdk                    8.2.3
@angular/http                   7.2.16
@schematics/angular             11.0.5
@schematics/update              0.1100.5
ng-packagr                      11.0.3
rxjs                            6.6.3
typescript                      4.0.5

Anything else relevant?

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: formsneeds reproductionThis issue needs a reproduction in order for the team to investigate further

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions