0

I have problem with error: Error: formGroup expects a FormGroup instance. Please pass one in. With this error I have parallel other error: Cannot read property 'get' of undefined. I tried resolved this errors but still without results

My code:

component.html

<form [formGroup]="captchaForm" (submit)="submit()" class="form">

  <mat-form-field appearance="fill" class="form-group">
    <mat-label>Site Code</mat-label>
    <input
      matInput
      id="handle"
      type="text"
      formControlName="handle"
      autofocus required
    >
  </mat-form-field>

  <mat-form-field appearance="fill" class="form-group">
    <mat-label>Code from image</mat-label>
    <input
      matInput
      id="token"
      type="text"
      formControlName="token"
      autofocus required
    >
  </mat-form-field>

  <button mat-raised-button class="form-button" color="primary" type="submit">Submit</button>

</form>

component.ts

captchaForm: FormGroup;
captchaData: Captcha;
dataDisplay = false;
    
constructor(
    public formBuilder: FormBuilder,
private captchaService: CaptchaService,
){}

ngOnInit(): void {
this.getCaptcha();
this.captchaForm = new FormGroup({
      token: new FormControl(),
      handle: new FormControl(this.captchaData.handle),
    });
}

submit() {
    this.captchaService.postCaptcha(this.captchaForm.value)
      .subscribe( (response: any) => {
      this.captchaData = response;
    });
  }
public getCaptcha() {
    this.captchaService.getCaptcha().subscribe(response => {
      this.captchaData = response;
      this.dataDisplay = true;
      console.log(response);
    });
  }

model.ts

export class Captcha {
  handle: string;
  imgCaptcha: string;
  token: string;
}

captcha.service.ts

postCaptcha(token: Captcha) {
    return this.http.post(environment.ApiUrl + `/api/url`, token, {responseType: 'text'});
  }

getCaptcha() {
    return this.http.get<any>(environment.ApiUrl + `/api/urlCaptcha`);
  }

Do you know how resolve this? Thanks

3
  • You have to imports ReactiveFormsModule where you already imported FormsModule Commented Mar 23, 2021 at 10:44
  • I already have them imported in modules. Commented Mar 23, 2021 at 10:50
  • Your code is working on stackblitz (I've not include Material). Try to remove some formControl one by one, and test. Commented Mar 23, 2021 at 10:53

1 Answer 1

1

Your FormGroup isn't initialised by the time you are using it. So remove it from ngOnInit

captchaData: Captcha;
captchaForm: FormGroup = new FormGroup({
  token: new FormControl(),
  handle: new FormControl()
});

use patchValue to update your form value:

public getCaptcha() {
  this.captchaService.getCaptcha().subscribe(response => {
  this.captchaData = response;
  this.dataDisplay = true;
  console.log(response);
  this.captchaForm.patchValue({
     handle: this.captchaData.handle
    });
 });
}
Sign up to request clarification or add additional context in comments.

7 Comments

I removed this code from ngOnInit and relocated this code after captchaData: Captcha; and this errors are resolved but now i have another Error: TypeError: Cannot read property 'handle' of undefined ... it's something bad with this row handle: new FormControl(this.captchaData.handle),?
from another api response I get handle data and I need insert this handle data to input.
after click on submit I get response 500, token looks like good because it's sending data but handle sending null data not insert data from captchaData.handle
post the code from where u are getting value for captchaData
check now and after submitting why are u setting the value to captcha data again
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.