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

Commit

Permalink
fix(input): make md-maxlength validation happen on initialization wit…
Browse files Browse the repository at this point in the history
…h interpolated value (#11338)

Closes #11329


## PR Checklist
Please check that your PR fulfills the following requirements:
- [x] The commit message follows [our guidelines](https://github.com/angular/material/blob/master/.github/CONTRIBUTING.md#-commit-message-format)
- [x] Tests for the changes have been added or this is not a bug fix / enhancement
- [x] Docs have been added, updated, or were not required

## PR Type
What kind of change does this PR introduce?
```
[x] Bugfix
[ ] Enhancement
[ ] Documentation content changes
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Infrastructure changes
[ ] Other... Please describe:
```

## What is the current behavior?

Using md-maxlength="MAX" on an input field that initializes to some value of length > "MAX", the md-maxlength validation does not happen, and the input field is considered valid.

Issue Number: #11329 

## What is the new behavior?

The validation now happens with specified value and error is detected

## Does this PR introduce a breaking change?
```
[ ] Yes
[x] No
```
<!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. -->
<!-- Note that breaking changes are highly unlikely to get merged to master unless the validation is clear and the use case is critical. -->

## Other information
  • Loading branch information
feloy authored and Splaktar committed Jul 31, 2018
1 parent a9c8fea commit 6896f01
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/components/input/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@ function mdMaxlengthDirective($animate, $mdUtil) {
var ngTrim = angular.isDefined(attr.ngTrim) ? $mdUtil.parseAttributeBoolean(attr.ngTrim) : true;
var isPasswordInput = attr.type === 'password';

scope.$watch(attr.mdMaxlength, function(value) {
maxlength = value;
});

ngModelCtrl.$validators['md-maxlength'] = function(modelValue, viewValue) {
if (!angular.isNumber(maxlength) || maxlength < 0) {
return true;
Expand Down Expand Up @@ -685,7 +689,6 @@ function mdMaxlengthDirective($animate, $mdUtil) {
});

scope.$watch(attr.mdMaxlength, function(value) {
maxlength = value;
if (angular.isNumber(value) && value > 0) {
if (!charCountEl.parent().length) {
$animate.enter(charCountEl, errorsSpacer);
Expand Down
36 changes: 36 additions & 0 deletions src/components/input/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,42 @@ describe('md-input-container directive', function() {
expect(getCharCounter(el).text()).toBe('5 / 5');
});

it('should error with an interpolated value and incorrect initial value', function() {
var el = $compile(
'<form name="form">' +
' <md-input-container>' +
' <input md-maxlength="mymax" ng-model="foo" name="foo">' +
' </md-input-container>' +
'</form>')(pageScope);

pageScope.$apply('mymax = 8');
pageScope.$apply('foo = "ABCDEFGHIJ"');

// Flush any pending $mdUtil.nextTick calls
$timeout.flush();

expect(pageScope.form.foo.$error['md-maxlength']).toBe(true);
expect(getCharCounter(el).text()).toBe('10 / 8');
});

it('should work with an interpolated value and correct initial value', function() {
var el = $compile(
'<form name="form">' +
' <md-input-container>' +
' <input md-maxlength="mymax" ng-model="foo" name="foo">' +
' </md-input-container>' +
'</form>')(pageScope);

pageScope.$apply('mymax = 5');
pageScope.$apply('foo = "abcde"');

// Flush any pending $mdUtil.nextTick calls
$timeout.flush();

expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
expect(getCharCounter(el).text()).toBe('5 / 5');
});

it('should work with a constant', function() {
var el = $compile(
'<form name="form">' +
Expand Down

0 comments on commit 6896f01

Please sign in to comment.