Here’s the structure I use. It’s not all of the code, but it gives you the idea.
Code:
import { Validators, FormBuilder, FormGroup } from '@angular/forms';
export class MyPage {
form : FormGroup;
// Validation error messages that will be displayed for each form field with errors.
validation_messages = {
'myField': [
{ type: 'pattern', message: 'Please enter a number like 12345678/00.' }
]
}
constructor(public formBuilder: FormBuilder) {
// Create the form and define fields and validators.
this.form = this.formBuilder.group({
myField: ['', Validators.pattern('[0-9]{8}/[0-9]{2}')]
});
// Initial value for the field.
this.form.get('myField').setValue('11223344/55');
}
/*
* Save form values.
*/
save() {
if (this.form.valid) {
// Save your values, using this.form.get('myField').value;
}
}
Error: Uncaught (in promise): Error:
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>
Error:
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>
at Function.TemplateDrivenErrors.modelParentException (http://localhost:8100/build/vendor.js:30983:15)
at NgModel._checkParentType (http://localhost:8100/build/vendor.js:31341:34)
at NgModel._checkForErrors (http://localhost:8100/build/vendor.js:31325:18)
at NgModel.ngOnChanges (http://localhost:8100/build/vendor.js:31209:14)
at checkAndUpdateDirectiveInline (http://localhost:8100/build/vendor.js:12781:19)
at checkAndUpdateNodeInline (http://localhost:8100/build/vendor.js:14309:20)
at checkAndUpdateNode (http://localhost:8100/build/vendor.js:14252:16)
at debugCheckAndUpdateNode (http://localhost:8100/build/vendor.js:15145:76)
at debugCheckDirectivesFn (http://localhost:8100/build/vendor.js:15086:13)
at Object.eval [as updateDirectives] (ng:///AppModule/RegistrationPage.ngfactory.js:278:5)
at Function.TemplateDrivenErrors.modelParentException (http://localhost:8100/build/vendor.js:30983:15)
at NgModel._checkParentType (http://localhost:8100/build/vendor.js:31341:34)
at NgModel._checkForErrors (http://localhost:8100/build/vendor.js:31325:18)
at NgModel.ngOnChanges (http://localhost:8100/build/vendor.js:31209:14)
at checkAndUpdateDirectiveInline (http://localhost:8100/build/vendor.js:12781:19)
at checkAndUpdateNodeInline (http://localhost:8100/build/vendor.js:14309:20)
at checkAndUpdateNode (http://localhost:8100/build/vendor.js:14252:16)
at debugCheckAndUpdateNode (http://localhost:8100/build/vendor.js:15145:76)
at debugCheckDirectivesFn (http://localhost:8100/build/vendor.js:15086:13)
at Object.eval [as updateDirectives] (ng:///AppModule/RegistrationPage.ngfactory.js:278:5)
at c (http://localhost:8100/build/polyfills.js:3:19752)
at Object.reject (http://localhost:8100/build/polyfills.js:3:19174)
at NavControllerBase._fireError (http://localhost:8100/build/vendor.js:51705:16)
at NavControllerBase._failed (http://localhost:8100/build/vendor.js:51698:14)
at http://localhost:8100/build/vendor.js:51745:59
at t.invoke (http://localhost:8100/build/polyfills.js:3:14976)
at Object.onInvoke (http://localhost:8100/build/vendor.js:5134:33)
at t.invoke (http://localhost:8100/build/polyfills.js:3:14916)
at r.run (http://localhost:8100/build/polyfills.js:3:10143)
at http://localhost:8100/build/polyfills.js:3:20242