bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Form Reset and Typeahead Bug

Open acgentry opened this issue 11 years ago • 5 comments

If you set your typeahead to restrict to list (typeahead-editable="false) and the user types in garbage, resetting the form using a new model (model = {}) does not clear the typeahead control.

Here is a plnkr.

It does clear the control if the user selects a correct item in the array or the editability is turned on.

There seems to be other issues like this, but not exactly related since those seem to be angular 1.3 related and it looks like this is a problem in 1.2 as well.

acgentry avatar Dec 04 '14 16:12 acgentry

I wonder what kind of behaviour was actually intended with typeahead-editable="false"?

It indeed does not prevent user from typing garbage. However, not letting user type for no visible reason isn't user-friendly.

Even more problems restricting user edit would come from async loading...

dmitriz avatar Apr 15 '15 00:04 dmitriz

When typeahead is used with configuration typeahead-editable="false", any garbage value entered by user sets the value of typeahead model to undefined.

The approach used in the demo plunk tries to reset the values in the form by setting the corresponding value of the model to undefined again. As there is no actual change in the value of the model, the ngModel $formatters are not invoked which would have cleared the view, hence no change occurs in value being displayed.

The typeahead widget is not able to determine if any change has happened in the model, because for the widget, the value has not changed at all. It is still undefined.

In order to reset the model values, we can modify the resetting logic used in the plunk and explicitly set the value for the typeahead model to null as in this sample plunk.

ankit5990 avatar Apr 17 '15 07:04 ankit5990

There seem to be no straight forward way to fix this issue.

The typeahead directive reads the input value and tries to assign a model value to it by using ngModel.$parsers. In case when model value can only be selected from the typeahead dropdown, the parser returns undefined to mark the input parsed value as invalid-parsed.

As mentioned in angular docs, in such a case, the model is assigned a value undefined.

Now if we use some logic to reset the value of the typeahaed by reassigning it undefined, it will not work as no watches, formatters, etc will be invoked (as no actual change in value has occured).

ankit5990 avatar Apr 17 '15 07:04 ankit5990

Agreed. This isn't restricted to just typeahead. Any form input that uses a parser would have this same issue. I believe this is by design (by AngularJS) though. As @ankit5990 mentioned, you need to set the model value explicitly to null.

Thanks for looking into this @ankit5990 . :+1:

chrisirhc avatar Apr 17 '15 07:04 chrisirhc

:) my pleasure... I am glad you found the analysis useful

ankit5990 avatar Apr 18 '15 18:04 ankit5990