Form model doesn't update on autocomplete
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:
- Enter data into the form a few times so that auto-complete is activated.
- Then enter a few characters that triggers an auto-complete.
- Hit return.
- 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.
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.
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.
I proposed this solution 4 months ago lol. http://victorblog.com/2014/01/12/fixing-autocomplete-autofill-on-angularjs-form-submit/
@evictor @edzius Can you please explain here how your solution compares to https://github.com/angular/angular.js/issues/1460#issuecomment-34364374 ?
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?
@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.
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();
});
});
}
};
}])
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 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 thanks! the gist is now updated accordingly
I used the solution by @evictor - works out of the box - thanks.
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.
@ryanmc2033 I agree 100%. Will someone please implement and submit PR? :)
@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.
@ryanmc2033 it's not enough. Some apps want check values on filling, not on submit.
@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.
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?
I add the following row experimentally and it worked on IE 8!
if ($sniffer.hasEvent('propertychange')) { element.on('propertychange', deferListener); }
@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"
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.