-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Bug when using ngModel, $asyncValidators and SELECT elements. #8929
Comments
No, I don't think so. This is is something else. This happens because @petebacondarwin WDYT? (this is somewhat remotely related to cdc7db3f) |
@shahata - without looking deeply into this, calling |
I'm not sure this is the way to go, and it is not resolving the issue in all the cases. When you change a value in the select, the async validators are triggered three times instead of only once. UPDATE: in the plnkr, the changes in the select triggers its own async validation, then it triggers input async validation and then the select async validation is triggered a second time. Actually, I find it very difficult to determine what the expected behavior should be when both the input and the select have async validations on the same model value. http://plnkr.co/edit/EDuXlZopgARZ93kYSvGy?p=preview |
@petebacondarwin hmm, but if Truth is I'm a bit confused by this |
Glad this seems to be getting traction - when/if you do get a satisfactory fix, be sure to double check that selecting 0 (zero) works as when I quickly hacked my angular copy to fix this issue, I found that another bug stopped the selection of zero. Let me know if there's anything else I can do. |
@maximumduncan you can play around with this to see if your issue is resolved: http://plnkr.co/edit/3CDLMe?p=preview |
@shahata - yes, that does seem to resolve the original bug thanks. However, I disagree on the viability of your workaround for the "0 becomes empty string thing" because normally angular has no trouble when using zeros in selects. In your Plunk, if you remove the |
Yes, I agree that this should be reported as a different issue. The workaround is just that - a way to work around the issue. |
Fair enough - I was going to raise it as an issue, but I hesitated because it's not actually a reproducible issue until this Issue's bug is resolved. What would you suggest is the correct play here? |
@caitp Could you review this? |
This behavior is wrong for two reasons. 1/ after validation the right value should be set It's interesting that the text input doesn't have these issues at all as you can see in the username field of the @matsko's demo app: http://yearofmoo-angularjs-forms.herokuapp.com/#/ I'd expect the username to be also temporarily set to the old value or empty string while the validation is happening (which would be wrong behavior but at least consistent with the select behavior) And the 0 bug is also real. I don't know it's cause yet. |
#8937 seems like the right fix with that fix the 0 bug is also fixed. |
Closes angular#8929 Conflicts: src/ng/directive/select.js
I've confirmed that both issues reported in here are fixed my end when using 1.3.0-rc.5. Thanks a lot guys :) |
I'd love to reopen this. I'm able to recreate ng-model breaking in Angular 1.4.8 when using async validators, this time using just input text elements. Again the issue is when asyncValidators are added, changes to ng-model aren't ran when the viewValue changes. Version: 1.4.8 |
I also went further and added asyncValidators by creating a new directive that accesses a required ngModel controller, as suggested by a coworker since that's the exact example defined in the documentation for forms. It surprisingly still breaks ng-model actually. |
@jfarid27 - I don't understand your use case. In both examples you return a promise that is never resolved, so of course the value never becomes valid, and so the model is not updated. |
@petebacondarwin yeah it seems to be the case. It was a little unintuitive but I wanted an async validation that does not immediately make requests to resolve and exposes the deferred objects for a separate service to resolve. Maybe there's a case for this, but it seems like more work to implement and a bad idea. |
You should be able to do this. You can create a service that does the work and returns a promise to the async validator. When the promise is resolved by the service the validator will resolve too |
@petebacondarwin yes but I'd also love to see model view changes. I'm trying to do a "bulk" validation in one API call after a user fills out all their form elements then clicks a button. The moment I add an $asyncValidator, there are no longer any $modelView updates. I figured I would try to use the $asyncValidators, but it seems to serve only the specific purpose of validating model immediately and not allowing any updates to the $modelView until the promise is resolved. |
Tested and reproduced with version: 1.3.0-rc.0
If you use the relatively new $asyncValidators feature with SELECT elements bound to an ngModel the SELECT options become unselectable.
Please see the following plunk:
http://plnkr.co/edit/wlBKH2
This seems to be because
$asyncValidators
set the$modelValue
toundefined
while the async validators are running, but never set it back again to the actual value once all the async validators pass.The text was updated successfully, but these errors were encountered: