I was trying to implement what seemed to be a straightforward form validation routine following this walk-through on my ionic 2 projects.
In my controller's constructor I used the FormBuilder
like this to make a formGroup:
this.form = formBuilder.group({
date: ['', Validators.required],
client: ['', Validators.required]
});
And then in the template I added formControllerName
attribute to relevant elements like this:
<ion-select formControlName="client" [(ngModel)]="clientId">
And bound the root element to the 'formGroup` like this:
<ion-content [formGroup]="form">
This fails with the following error message:
ngModel cannot be used to register form controls with a parent formGroup directive. Try using formGroup's partner directive "formControlName" instead. Example:
<div [formGroup]="myGroup"> <input formControlName="firstName"> </div> In your class: this.myGroup = new FormGroup({ firstName: new FormControl() }); Or, if you'd like to avoid registering this form control, indicate that it's standalone in ngModelOptions: Example: <div [formGroup]="myGroup"> <input formControlName="firstName"> <input [(ngModel)]="showMoreControls" [ngModelOptions]="{standalone: true}"> </div>
As suggested by the error message, as well as this StackOverflow thread I added [ngModelOptions]="{standalone: true}"
to my inputs but it came back with another error message:
Template parse errors: Can't bind to 'ngModelOptions' since it isn't a known property of 'ion-select'.