Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Inconsistent handling of invalid initial ngModel values #8949

Closed
@Narretz

Description

@Narretz

An ngModelController created with an invalid inital value from scope behaves differently depending on input type:

http://jsfiddle.net/PK3QH/27/

Type invalid model view behavior error good
text {} [object Object] calls toString correctly set ⁉️
url 'notanurl' notanurl calls toString correctly set
number 'notanumber' (empty) ❌ throws if model is not type number ⁉️ no errors are set because of exception ❌
number+min=5 3 3 ^ correctly set
date 'notadate' (empty) ❌ expects a date and uses the date filte, else returns nothing required set
date new Date('123456') 0NaN-NaN-NaN ^ $error.date is not set ❌

I think we should always try to set the $viewValue to something. This is important if you have server-side data that may be invalid.
So number and date should not discard invalid values. If we pass garbage to the validators, the correct errors will be set.
Regarding calling toString on text-based input. I think that's okay as you will rarely set an object to a model when you know it'll be displayed in an input. But we should do "the right thing" and try to format values that are good enough, so in short:

  • number should not throw and call toString regardless
  • date should return call toString if not a Date object
  • date should also get the bug fixed where NaN gets output

I'll think I'll be able to throw a PR together over the weekend.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions