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

Commit 0cb4af1

Browse files
feloymmalerba
authored andcommitted
fix(input): make md-maxlength validation happen on initialization with 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
1 parent 2cc372d commit 0cb4af1

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/components/input/input.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,10 @@ function mdMaxlengthDirective($animate, $mdUtil) {
642642
var ngTrim = angular.isDefined(attr.ngTrim) ? $mdUtil.parseAttributeBoolean(attr.ngTrim) : true;
643643
var isPasswordInput = attr.type === 'password';
644644

645+
scope.$watch(attr.mdMaxlength, function(value) {
646+
maxlength = value;
647+
});
648+
645649
ngModelCtrl.$validators['md-maxlength'] = function(modelValue, viewValue) {
646650
if (!angular.isNumber(maxlength) || maxlength < 0) {
647651
return true;
@@ -685,7 +689,6 @@ function mdMaxlengthDirective($animate, $mdUtil) {
685689
});
686690

687691
scope.$watch(attr.mdMaxlength, function(value) {
688-
maxlength = value;
689692
if (angular.isNumber(value) && value > 0) {
690693
if (!charCountEl.parent().length) {
691694
$animate.enter(charCountEl, errorsSpacer);

src/components/input/input.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,42 @@ describe('md-input-container directive', function() {
291291
expect(getCharCounter(el).text()).toBe('5 / 5');
292292
});
293293

294+
it('should error with an interpolated value and incorrect initial value', function() {
295+
var el = $compile(
296+
'<form name="form">' +
297+
' <md-input-container>' +
298+
' <input md-maxlength="mymax" ng-model="foo" name="foo">' +
299+
' </md-input-container>' +
300+
'</form>')(pageScope);
301+
302+
pageScope.$apply('mymax = 8');
303+
pageScope.$apply('foo = "ABCDEFGHIJ"');
304+
305+
// Flush any pending $mdUtil.nextTick calls
306+
$timeout.flush();
307+
308+
expect(pageScope.form.foo.$error['md-maxlength']).toBe(true);
309+
expect(getCharCounter(el).text()).toBe('10 / 8');
310+
});
311+
312+
it('should work with an interpolated value and correct initial value', function() {
313+
var el = $compile(
314+
'<form name="form">' +
315+
' <md-input-container>' +
316+
' <input md-maxlength="mymax" ng-model="foo" name="foo">' +
317+
' </md-input-container>' +
318+
'</form>')(pageScope);
319+
320+
pageScope.$apply('mymax = 5');
321+
pageScope.$apply('foo = "abcde"');
322+
323+
// Flush any pending $mdUtil.nextTick calls
324+
$timeout.flush();
325+
326+
expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
327+
expect(getCharCounter(el).text()).toBe('5 / 5');
328+
});
329+
294330
it('should work with a constant', function() {
295331
var el = $compile(
296332
'<form name="form">' +

0 commit comments

Comments
 (0)