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

@jamm I need it on a page where the user can input an arbitrary amount of addresses and thus can summon a theoretically infinite amount of address forms (which are actual directives themselves). With four such forms on screen and 6 autofillables in each, my CPU spiked out. I'm sure it'll be fine for some people though, good stuff either way. Any solution is a good solution at this point, even if it means setting the entire form's autocomplete to off :-|

Swader avatar May 28 '13 21:05 Swader

@Swader "infinite" and "setTimeout" should not be together, definitely :)

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

We should make the distinction between autofill and autocomplete:

  • Autofill happens at page load in login forms, the browser pre-populates the form with the saved login and password. Autofill also happens in a contact form, when you start typing your name or email, some browsers (e.g. Safari) propose to complete the form based on our personal contact card from the address book.
  • Autocomplete happens when you start typing the same text that has been previously submitted in the same input. Then the browser proposes the previous text that has been submitted. You may or may not use this text.

The autocomplete issue might be easier to fix, since we can trigger a model checking at the blur event. Here is a quick fix:

module.directive('input', [
    function () {
        return {
            restrict: 'E',
            require: '?ngModel',
            link: function ($scope, element, attrs, controller) {
                if (!attrs.type || attrs.type !== "text" || !controller) {
                    return;
                }
                element.on('blur', function () {
                    $scope.$apply(function () {
                        controller.$setViewValue(element.val());
                    });
                });
            }
        };
    }
]);

I don't know if the autofill issue can be easily fixed, since the browser don't seem to trigger any event during autofill (http://avernet.blogspot.in/2010/11/autocomplete-and-javascript-change.html).

tleruitte avatar Jun 17 '13 14:06 tleruitte

I'm talking about auto-fill. Thanks for clarification.

e-oz avatar Jun 17 '13 14:06 e-oz

I've posted a $100 bounty to this issue at Bountysource: https://www.bountysource.com/issues/67567-form-model-doesn-t-update-on-autocomplete

The bounty will go to the person whose pull request closes this issue.

I'd like to see this issue addressed because it affects Bountysource's signin, bountysource/frontend#78

rappo avatar Jul 18 '13 21:07 rappo

+1

wvdhaute avatar Jul 26 '13 14:07 wvdhaute

My solution is to extend the 'required' directive or whichever validation directive that might be used. I listen for the respective events that each browser throws when autocomplete is made and trigger the 'input' event.

http://jsfiddle.net/matthewgonzalez/fAmHD/10/

matthewgonzalez avatar Jul 30 '13 21:07 matthewgonzalez

For us the best choice is to create a new directive at the same level than the form directive with higher priority to be executed before the controller actions and we have subscribed the event submit to refresh the value from the context before to call the actions in the controllers. At this way we always have the last values in our scope before to execute any action.

priority: 10,
link: function ($scope, element, attrs) {
//Extract
    element.on('submit', function (ev) {
      $('input[ng-model]', element).each(function (index, item) {
         if (angular.element(this).attr('type') !== 'checkbox' && angular.element(this).attr('type') !== 'radio') {
         angular.element(this).controller('ngModel').$setViewValue($(this).val());
       }
     });
   });
//Extract

mingue avatar Sep 13 '13 15:09 mingue

@mingue the directive approach is nice!

dmitrybelyakov avatar Sep 14 '13 11:09 dmitrybelyakov

@mingue thanks, that's the best solution / workaround so far. solved my problems with autofill passwords.

tech-no-logical avatar Sep 20 '13 13:09 tech-no-logical

@mingue Thanks for this directive, works really well.

Also, it seems like the bug happens in IE only when the user tries to select an autofill proposition with the mouse. I wasn't able to reproduce the bug when selecting a proposition with the keyboard, which didn't help to clearly identify it.

pjparra avatar Sep 23 '13 09:09 pjparra

@matthewgonzalez Thanks for your solution! I've renamed the directive so is not replacing the 'required' validation. So far is working great.

jordins avatar Sep 24 '13 07:09 jordins

+1

dkleehammer avatar Oct 14 '13 22:10 dkleehammer

For the username/password autofill issue:

This may be a browser issue as well, I just opened a bug for Chromium:

https://code.google.com/p/chromium/issues/detail?id=307308

Meanwhile, I am using this little workaround:

$(window).load(function() {
  // updates autofilled fields
  window.setTimeout(function() {
    $('input[ng-model]').trigger('input');
  }, 100);
});

It's not very nice, if I use 2 ms instead of 100 ms it doesn't always work, which means that the autofill is performed several ms after the page has been loaded. I don't see any deterministic way to handle this problem.

syenchuk avatar Oct 15 '13 00:10 syenchuk

For me this does the trick on FF 24.0

app.directive("autofill", function () {
    return {
        require: 'ngModel',
        link: function (scope, element, attrs, ngModel) {
            scope.$watch(function () {
              return element.val();
            }, function(nv, ov) {
              if(nv !== ov)
                ngModel.$setViewValue(nv)
            })
        }
    }
});

Should be tested on other browsers, but I see no reason why it should not work... I see a couple of reasons you shouldn't use it though...

gplancke avatar Oct 26 '13 18:10 gplancke

+1

stevenboone avatar Nov 07 '13 03:11 stevenboone

+1

ghost avatar Nov 08 '13 19:11 ghost

I'm having a problem with the workaround proposed by @mingue above. It seems to work fine in 1.0.8, but fails on 1.2.0. It seems the on('submit') never gets triggered. Does anybody have any idea as to why this is happening ?

edit : this plunk demonstrates the problem. in 1.0.8 the directive gets called first. in 1.2.0 it gets called last.

http://plnkr.co/edit/B1wWKkTdk5QHircmUdu2?p=preview

ideas ?

tech-no-logical avatar Nov 11 '13 20:11 tech-no-logical

+1

noducks avatar Nov 16 '13 01:11 noducks

@tech-no-logical : Did you try the code of @gplancke ? It looks perfect.

The workaround proposed by @mingue is limited : Without a submit event it's impossible to access the entry.

GabrielDelepine avatar Nov 17 '13 09:11 GabrielDelepine