I'm working with ionic 5 and Angular 9. I'm trying to create a reactive form but I got the error 'No value accessor for form control with name: 'lastname''.
Here is my code :
export class ModalComponent implements OnInit {
public form: FormGroup;
constructor(private modalController: ModalController,
private formBuilder: FormBuilder) { }
ngOnInit(): void {
this.initForm();
}
public close(): void {
// using the injected ModalController this page
// can "dismiss" itself and optionally pass back data
this.modalController.dismiss({
'dismissed': true
});
}
public initForm(): void {
this.form = this.formBuilder.group({
firstname: ['', Validators.required],
lastname: ['', Validators.required]
});
}
logForm(){
console.log(this.form.value)
}
}
<form [formGroup]="form" (ngSubmit)="logForm()" novalidate>
<ion-item>
<ion-label>Last name</ion-label>
<ion-input type="text" formControlName="lastname"></ion-input>
</ion-item>
</form>
Edit : I just found the problem. I was missing the import IonicModule in my module.
public async presentModal(): Promise<void> { const modal = await this.modalController.create({ component: ModalComponent }); return await modal.present(); }
– Cutback