angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Form model doesn't update on autocomplete

Open msgilligan opened this issue 14 years ago • 198 comments

I'm having problems with Safari 6.0 auto-complete on some simple forms on Angular 1.0.2.

When Safari uses auto-complete to fill in the form, Angular seems to be unaware of the new characters that were entered by auto-complete.
Please see the attached image for an example.

To reproduce:

  1. Enter data into the form a few times so that auto-complete is activated.
  2. Then enter a few characters that triggers an auto-complete.
  3. Hit return.
  4. Compare the alert message with the contents of the input field.

I've created a jsFiddle that can be used to reproduce the problem: http://jsfiddle.net/msgilligan/5cynT/

I've seen a similar problem when Safari 6.0 or Chrome 22.0.1229.79 automatically fills in a password field that has a 'required' attribute. The form is not marked as valid and I have to go enter a space after the password and then delete it.

msgilligan avatar Oct 15 '12 17:10 msgilligan

Triggered by what browser event?

On Thu, May 23, 2013 at 8:53 AM, Mark Shercliff [email protected]:

YUI has a solution in its own framework - implementing its own event:

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1460#issuecomment-18341083 .

davemerrill avatar May 23 '13 15:05 davemerrill

From what I've seen, currently only input events trigger a scope update, but autofill implementations tend to trigger the change event. I'm not sure what the reasoning behind using input over change is - maybe somebody can shine some light on this?

arikal avatar May 23 '13 15:05 arikal

+1 this is essential and it's ridiculous that this issue has been opened for 7 months now. Please fix asap.

Swader avatar May 26 '13 00:05 Swader

Pull request #2129 may well be a solution to this issue, as it allows additional or alternative events to trigger updates.

symblify avatar May 26 '13 00:05 symblify

Pull request #2129 is a solution, but it's hack, it's not normal behavior for framework.

cmygeHm avatar May 26 '13 06:05 cmygeHm

Guys, I find it really sad when you say things like "it's ridiculous that this issue hasn't been fixed yet". If you really cared about this issue, you'd fix it already and submitted a patch. Accusing others of lack of action when you are not doing anything either is ridiculous.

Having said that, nobody on this thread actually mentioned that this is a weird browser/spec issue that is caused by the input event not being fired upon autofil/autocompletion. Has anyone actually tried to figure out what event should we listen on in order to get notified of the new value? I've seen only one comment suggesting that change event might be the right one to listen. But if you ever worked with the change event you'd know that it's not a great choice in many other situations. So as you might have guessed the solution is not trivial and it needs to be well tested. Does anybody want to step up and offer a proper fix?

IgorMinar avatar May 26 '13 15:05 IgorMinar

oh.. so maybe the change event doesn't work so well.. http://furrybrains.com/2009/01/02/capturing-autofill-as-a-change-event/

IgorMinar avatar May 26 '13 15:05 IgorMinar

There must be some kind of event that is fired on the node when the autofill/autocompletion happens, we should listen for that event and treat it as input event.

IgorMinar avatar May 26 '13 16:05 IgorMinar

Given the multitude of ways to change an input without (for all we know) any event triggering, such as autocomplete and setting .value with JS, I would say it makes sense to at least provide a generic way to "force read values" as I outlined in my comment some time ago. For example:

$scope.$readValues()

ng-model would need to implement a new API which basically means a callback getting called that sets the scope values by reading the input, just as the normal DOM event listening does.

This is obviously not ideal since it means users has to remember to call readValues. But at least it would proivde a way to solve the problem.

Note that Angular could probably call readValues() on any form's submit event. This would mitigate having to remember to call it by hand, and I would assume most programmers wrap inputs in form tags anyway, in order to piggyback on default browser behaviour for pressing return on the keyboard, etc. On May 26, 2013 6:06 PM, "Igor Minar" [email protected] wrote:

There must be some kind of event that is fired on the node when the autofill/autocompletion happens, we should listen for that event and treat it as input event.

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1460#issuecomment-18465236 .

augustl avatar May 26 '13 16:05 augustl

@augustl that won't work because we need to be able to trigger model changes by autocomplete. just forcing read from the dom when some other model changes is an incorrect approach as it introduces inconsistency into the system.

IgorMinar avatar May 26 '13 16:05 IgorMinar

Other than polling, it is the only solution I can think of if it turns out that there are ways to change input values without triggering DOM events (such as auto complete and setting .value on input elements). On May 26, 2013 6:49 PM, "Igor Minar" [email protected] wrote:

@augustl https://github.com/augustl that won't work because we need to be able to trigger model changes by autocomplete. just forcing read from the dom when some other model changes is an incorrect approach as it introduces inconsistency into the system.

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1460#issuecomment-18465838 .

augustl avatar May 26 '13 16:05 augustl

Can't we just forcibly implement the re-check on submit if we're dealing with a form, and ignore the situation when people are using inputs outside forms? There's a lot of such cases going around, it would be just one more "if you're using the form, you're additionally protected against the autocomplete bug because Angular automatically re-syncs the models in case there's autocomplete filled values". If you use input fields without form, pressing enter won't submit either, so it's not normal form behavior and as such, the developer has no right to expect autocomplete to matter anyhow because he has to be handling the input values through JS manually anyway.

When you say it's the browser's fault - the JSON/JSONP bug is the browser's fault too, yet we are still advised to prefix the string ")]}',\n" to every JSON string returned just to be safe.

I believe it would be practical for everyone this way: when using form, if you add an attribute recheck="true", this will make all of the form's children re-check their model values on submit.

It's not a perfect solution, but seeing as a perfect solution is impossible at this time, I think having any solution is better than having none. This way nothing is forced on anyone, and we can all work hack-free.

Swader avatar May 26 '13 21:05 Swader

Although onsubmit is a good case, many of us are using the evaluation to disable the submit button until the form is valid. In such a case, it would be great to be able to call validation when the page is ready. By exposing a call to trigger validation, everyone's validation problems would be solved. On May 27, 2013 12:43 AM, "Bruno Škvorc" [email protected] wrote:

Can't we just forcibly implement the re-check on submit if we're dealing with a form, and ignore the situation when people are using inputs outside forms? There's a lot of such cases going around, it would be just one more "if you're using the form, you're additionally protected against the autocomplete bug because Angular automatically re-syncs the models in case there's autocomplete filled values". If you use input fields without form, pressing enter won't submit either, so it's not normal form behavior and as such, the developer has no right to expect autocomplete to matter anyhow because he has to be handling the input values through JS manually anyway.

When you say it's the browser's fault - the JSON/JSONP bug is the browser's fault too, yet we are still advised to prefix the string ")]}',\n" to every JSON string returned just to be safe.

I believe it would be practical for everyone this way: when using form, if you add an attribute recheck="true", this will make all of the form's children re-check their model values on submit.

It's not a perfect solution, but seeing as a perfect solution is impossible at this time, I think having any solution is better than having none. This way nothing is forced on anyone, and we can all work hack-free.

— Reply to this email directly or view it on GitHubhttps://github.com/angular/angular.js/issues/1460#issuecomment-18470223 .

shmuel613 avatar May 27 '13 05:05 shmuel613

need this

godratio avatar May 27 '13 13:05 godratio

Wouldn't it be even easier if we just set a timeout to re-visit the models? Or will this cause problems to other code (unexpected triggers, race conditions etc.)

RWOverdijk avatar May 28 '13 05:05 RWOverdijk

If there were a way to revisit the models without actually traversing the DOM, that would be an acceptable hack I suppose. Traversing the DOM to find the inputs and re-sync the models wouldn't be acceptable, though.

Swader avatar May 28 '13 08:05 Swader

please somebody fix this. I'm ready to pay for this.

e-oz avatar May 28 '13 19:05 e-oz

Very dirty hack: http://stackoverflow.com/a/16800988/680786 but if somebody is forced to solve this issue by any cost - can be useful

e-oz avatar May 28 '13 20:05 e-oz

Bit too dirty for me, grinded the app to a halt when there's several forms with around 6-7 autofillables each. I'll try and make a decent onsubmit hack. It'll need DOM traversal looks like, but this is a serious gap that needs to be patched up with something, even if that something is mud and tar.

Swader avatar May 28 '13 21:05 Swader

@Swader 1) absolutely dirty. 2) it works fine with 6 fields, checked (and CPU level check also); 3) "onsubmit" is not enough :( I have form where some inputs should be validated by JS before submit.

e-oz avatar May 28 '13 21:05 e-oz