-
Notifications
You must be signed in to change notification settings - Fork 27.5k
fix(ngModel): don’t clear the model when an external validator failed #9016
Conversation
Calling `ctrl.$setValidity()` with a an error key that does not belong to a validator in `ctrl.$validator` should not result in setting the model to `undefined` on the next input change. This bug was introduced in 1.3.0-beta.12. Closes angular#8357 Fixes angular#8080
@@ -711,7 +727,7 @@ describe('NgModelController', function() { | |||
expect(ctrl.$pending).toBeUndefined(); | |||
})); | |||
|
|||
it('should clear and ignore all pending promises when the input values changes', inject(function($q) { | |||
it('should clear and ignore all pending promises when the model values changes', inject(function($q) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/changes/change
Added comments, otherwise LGTM. |
As I said, I don't like it. It introduces a distinction between validity set by $validators, and validity set externally. I think this is something that is gonna be difficult to explain to users. Let's not try to recreate the old behavior simply because it was there. |
We should at least wait and try to get some input from the original posters. But from a consistency and understandability POV this makes the API worse. |
@Narretz Without this change using |
Yeah, that makes sense. |
Aaaand, there's still no easy way to get the parsed model value, when (a) your model is invalid (but not because of $setValidity), and (b) you don't use allowInvalid - for example in a form submit handler. That's it, I think. |
@Narretz I don't understand that usecase: you want to get the invalid value, but don't want to use the |
It is important when you want only valid values on the scope, but still want to do some postprocesing in your form handling. (so you don't use allowInvalid). You submit a form, and find that a value is invalid. My problem is that it is strange that we simply throw away the parsed model value when it is invalid, and force you to use allowInvalid to get it. Restoring the validation behavior of $setValidity was basically a workaround for one specific use case. For me the API is incomplete without the parsed model value, or an easy way to get it. *Running formatters manually has been requested actually, so $runParsers would be one way to make this easier. |
So why would you not use |
allowInvalid sets the model on the scope, and this might be undesired if it is invalid. For example, you display your form inputs in another way, but you only want that for valid values. Or your model is watched on the scope. |
For reading
For that you actually would want to have the invalid value on the scope and not implement this logic in a directive... |
You don't really need a directive... It is available at |
Right! As the whole controller lives there... Never used it that way though... |
Or @shahata |
@tbosch it makes more sense to me that the invalid model value will be stored on |
Hey guys, I like where this is going! ;) |
I'm fine with the semantics of $rawModelValue, not sure about the name though, but we can sort that out later. The reason I don't like the idea of using $modelValue is that I assume some directives will still want the value that is actually assigned to the model. (which is also what the name implies) |
Calling
ctrl.$setValidity()
with a an error key thatdoes not belong to a validator in
ctrl.$validator
shouldnot result in setting the model to
undefined
on the nextinput change. This bug was introduced in 1.3.0-beta.12.
Closes #8357
Fixes #8080