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

maxlength directive doesn't work with numbers #8811

Closed
hon2a opened this issue Aug 28, 2014 · 4 comments
Closed

maxlength directive doesn't work with numbers #8811

hon2a opened this issue Aug 28, 2014 · 4 comments

Comments

@hon2a
Copy link

hon2a commented Aug 28, 2014

The maxlength directive is broken when used with Number model values in the current Beta. When I try using timepicker from angular-bootstrap, the hours/minutes inputs become invalid as soon as they reach values >= 10. This happens because, for the sake of readability, values < 10 are prefixed with a zero and saved to the model as strings. Values >= 10 are saved as numbers, which makes this offending line in the directive:

return ctrl.$isEmpty(value) || value.length <= maxlength;

fail, as value.length equals to undefined for Numbers. Changing the code to

return ctrl.$isEmpty(value) || String(value).length <= maxlength;

seems to work just fine.


Thanks in advance for the fix.

@Narretz
Copy link
Contributor

Narretz commented Aug 28, 2014

This is a duplicate of #7848

@Narretz
Copy link
Contributor

Narretz commented Aug 28, 2014

I'll try to get someone interested in this for the next beta / RC.

@Narretz Narretz closed this as completed Aug 28, 2014
@caitp
Copy link
Contributor

caitp commented Aug 28, 2014

a fix for this has already been written, it's never been reviewed or merged #7968

@Narretz
Copy link
Contributor

Narretz commented Aug 28, 2014

The PR in question is #7968
I pushed it to the next RC.

caitp added a commit to caitp/angular.js that referenced this issue Aug 28, 2014
Use viewValue for minlength/maxlength validation if model value is not a string.
This allows ngMinlength and ngMaxlength to be used for number inputs.

Closes angular#7967
Closes angular#8811
caitp added a commit that referenced this issue Aug 29, 2014
Use the viewValue rather than modelValue when validating. The viewValue should always be a string, and
should reflect what the user has entered, or the formatted model value.

BREAKING CHANGE:

Always uses the viewValue when validating minlength and maxlength.

Closes #7967
Closes #8811
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants