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

The execution of a function inside $timeout is synced with the angular digest cycle, so you don't have to call the $apply manually. IMO The $timeout method is the 'correct' one.

kumarharsh avatar Apr 28 '14 11:04 kumarharsh

In case autofill sync required on submit only, updated huel90 approach:

.directive('autofillSubmit', function () {
  'use strict';

  return {
    restrict: 'A',
    priority: -10,
    link: function (scope, element) {

      element.on('submit', function () {
        element.find('input, textarea, select').trigger('input').trigger('change').trigger('keydown');
      });

    }
  };
});

By using directive on <form> along with ng-submit, priority < 0 makes directive to be linked first so model get updated before ng-submit expression executed.

edzius avatar May 13 '14 15:05 edzius

I proposed this solution 4 months ago lol. http://victorblog.com/2014/01/12/fixing-autocomplete-autofill-on-angularjs-form-submit/

evictor avatar May 13 '14 18:05 evictor

@evictor @edzius Can you please explain here how your solution compares to https://github.com/angular/angular.js/issues/1460#issuecomment-34364374 ?

lgalfaso avatar May 13 '14 18:05 lgalfaso

Since browsers are supposed to dispatch "autocomplete" and "autocompleteerror" events now (and some do), it is probably a sensible idea for angular to handle them.

However, the HTML5 spec says autofilled content to not be validated, so we shouldn't run the validation pipeline when autocompleted, I think.

@matsko can we add something like that into the validation pipeline change?

caitp avatar May 13 '14 18:05 caitp

@lgalfaso it does exactly the same thing in a slightly different way. Instead of using priority to precede ngSubmit (which I admit is probably a cleaner way of doing this), it unbinds submit, then reattaches a new submit handle that A) triggers the appropriate events and B) $apply()s the nbSubmit expression.

evictor avatar May 13 '14 20:05 evictor

A "fix" for this bug using a modified version of the directive approach, since jqLite does not provide trigger function. Works fine for a login form.

    .directive('autofillSubmit', [function(){
        return {
            restrict: 'A',
            priority: -10,
            link: function( scope, element ){
                element.on('submit', function(){
                    _.forEach(element[0].querySelectorAll('input'), function( field ){
                        field.focus();
                        field.blur();
                    });
                });
            }
        };
    }])

gmoulin avatar May 26 '14 12:05 gmoulin

starting off @evictor solution, I made a form directive decorator for a seamless integration and no jQuery usage, here is the gist: https://gist.github.com/stefanotorresi/1de83e989fd780873af6 will add some tests later.

stefanotorresi avatar May 28 '14 10:05 stefanotorresi

@stefanotorresi Thx for your solution. Just a small detail, it seems JQlite find method is based on getElementsByTagName which can't select multiple tags as you suggested it in your gist (line 42).

akofman avatar Jun 05 '14 14:06 akofman

@akofman thanks! the gist is now updated accordingly

stefanotorresi avatar Jun 05 '14 15:06 stefanotorresi

I used the solution by @evictor - works out of the box - thanks.

swlasse avatar Jun 24 '14 12:06 swlasse

Can someone explain why angular can't just double check the actual form fields on ng-submit? This seems really easy and should have been implemented a long time ago.

justechn avatar Jun 26 '14 15:06 justechn

@ryanmc2033 I agree 100%. Will someone please implement and submit PR? :)

evictor avatar Jun 26 '14 17:06 evictor

@ryanmc2033 if you think it's easy, send a pull request =) Tbosch put together https://github.com/tbosch/autofill-event a while ago as a way to solve this, so you should be able to use that.

I thought it was linked in this issue, but I don't see it.

caitp avatar Jun 26 '14 17:06 caitp

@ryanmc2033 it's not enough. Some apps want check values on filling, not on submit.

e-oz avatar Jun 26 '14 20:06 e-oz

@jamm yes, but validation only becomes a problem when an autofilled form is submitted. Besides, @caitp said earlier "the HTML5 spec says autofilled content to not be validated,". So we should be skipping validation anyways.

justechn avatar Jun 26 '14 20:06 justechn

It seems that it has not been resolved yet.

There is a msdn link which explains we should use onpropertychange instead of onchange to capture autofill event.

http://msdn.microsoft.com/en-us/library/ms533032%28VS.85%29.aspx

http://msdn.microsoft.com/en-us/library/ms536956(v=vs.85).aspx

Is this helpful?

xzer avatar Jul 01 '14 10:07 xzer

I add the following row experimentally and it worked on IE 8!

if ($sniffer.hasEvent('propertychange')) { element.on('propertychange', deferListener); }

xzer avatar Jul 01 '14 11:07 xzer

@xzer the "propertychange" event is an IE thing, so this wouldn't benefit most users, or most of the supported browsers, and is very different from detecting "autofill"

caitp avatar Jul 01 '14 11:07 caitp

yes, it is a IE thing, so we need support it at least for IE8, I test it on IE8 and IE11, sorry I have only this two version in my machine, and it shows that IE11 don't have this event, which means "propertychange" is a IE 8 (or other old version) only event, which also means at least we can make IE 8 work correctly.

xzer avatar Jul 01 '14 11:07 xzer