-
Notifications
You must be signed in to change notification settings - Fork 26.7k
fix(forms): improve ngModel error messages #10314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
e9f5dd3
to
99e1d0d
Compare
@kara I came over this PR after a whole day spent investigating what broke our RC3 app after the upgrade to RC5. We were using ngModel to bind the data and FormGroup+FormControl to apply complex validation logic, like:
So far, in RC5, this approach no longer works. On one hand, we need to retain the validation logic, on the other we would really need to avoid rebuild the components' logic to get/set data through FormControls. What can we do to migrate effectively? |
@unsafecode The code snippet you posted is still supported and should actually work fine. What error are you getting? Can you post a sample plunker? |
@kara I'm getting the exact one at the top.
Trying to setup a plunker in the meanwhile |
@unsafecode The thing that doesn't make sense about that is it's an ngModel error: https://github.com/angular/angular/blob/master/modules/%40angular/forms/src/directives/template_driven_errors.ts. In the snippet you pasted, ngModel is not activated. Its selector excludes elements that have formControlName. Only formControlName should be activated. Are you sure that's the code that's causing the error? |
@kara Now I got it: there were some elements with the <div [formGroup]="myGroup">
<input name="firstName" [(ngModel)]="firstName">
</div> While the following, with the <div [formGroup]="myGroup">
<input formControlName="firstName" [(ngModel)]="firstName">
</div> |
Yep, the top code sample is deliberately not supported. formGroup expects you to create your own FormControl instances, whereas ngModel creates FormControl instances implicitly for you. You can't have two conflicting FormControl instances on the same form control element. When you use formControlName, ngModel does not activate or create a control (it's simply used as an @input). formControlName just links to the existing input you created in your class. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This PR adds some clearer error messages for invalid form states.
Case 1: Mixing formControlName with template driven form strategy
Before
Error: Cannot setParent of null
After
Case 2: Trying to use ngModel to register a form control with formGroup
Before
No error! But the form control will not behave predictably because the ngModel's control instance conflicts with the user's control instance.
After