Hi,
I have a number of forms in my app that I need to pre-populate with values from when the user signed in or registered. I can get these values to show in the view by using value="{{first name}}" etc on each input, but I cannot get form builder to see these values when submitting the form. Any advice would be appreciated.
Maintenance.ts:
export class MaintenancePage {
maintenance: any;
complaint: any;
userdata: any;
development: any;
unit_name: any;
type: any;
token: any;
firstname: any;
lastname: any;
email: any;
cell: any;
development_id: any;
constructor(
public navCtrl: NavController,
private formBuilder: FormBuilder,
public platform: Platform,
public actionsheetCtrl: ActionSheetController,
private userData: UserData,
private developmentService: DevelopmentService,
) {}
ionViewDidLoad() {
this.getUserData();
this.maintenance = this.formBuilder.group ({
firstname: [this.userdata.firstname],
lastname: [''],
email: [''],
cell: [''],
development: [''],
complaint: ['', Validators.required],
token: [this.userData.usertoken],
type: ['maintenance'],
});
console.log(this.maintenance.value);
}
getUserData() {
this.userData.getUserData().then((userdata) => {
this.userdata = userdata;
this.firstname = userdata.firstname;
this.lastname = userdata.lastname;
this.email = userdata.email;
this.cell = userdata.cell;
this.unit_name = userdata.unit_name;
this.loadDevelopments(userdata.development_id);
});
}
And Maintenance.html:
<ion-content id="maintenance-bg-img">
<ion-grid padding>
<form [formGroup]="maintenance" (ngSubmit)="maintenanceForm()">
<ion-item class="item">
<ion-label floating>First Name</ion-label>
<ion-input type="text" formControlName="firstname" value="{{firstname}}" readonly></ion-input>
</ion-item>
<ion-item class="item">
<ion-label floating>Last Name</ion-label>
<ion-input type="text" formControlName="lastname" value="{{lastname}}"></ion-input>
</ion-item>
<ion-item class="item">
<ion-label floating>Email Addess</ion-label>
<ion-input type="email" formControlName="email" value="{{email}}"></ion-input>
</ion-item>
<ion-item class="item">
<ion-label floating>Cell Number</ion-label>
<ion-input type="text" formControlName="cell" value="{{cell}}"></ion-input>
</ion-item>
<ion-item class="item">
<ion-label floating>Address</ion-label>
<ion-input type="text" formControlName="development" value="Unit {{unit_name}}, {{development}}"></ion-input>
</ion-item>
<ion-item class="item">
<ion-label floating>Complaint</ion-label>
<ion-textarea type="text" formControlName="complaint"></ion-textarea>
</ion-item>
<ion-item class="hidden">
<ion-textarea type="text" formControlName="token" value="{{token}}"></ion-textarea>
</ion-item>
<button ion-button color="dark" clear block type="submit" [disabled]="!maintenance.valid" id="maintenance-btn">Send your request</button>
</form>
So, I can see the values I want in the form, but cannot get them passed into the form builder instance in order to post them to an external api. And when I try this.userdata.firstname
it just returns inline template:11:7 caused by: formGroup expects a FormGroup instance. Please pass one in.