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

Commit 9ced357

Browse files
feloyjelbourn
authored andcommitted
fix(input): make md-maxlength validation happen on initialization (#11150)
Closes #10320
1 parent 87b3c3b commit 9ced357

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

src/components/input/input.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,25 @@ function mdMaxlengthDirective($animate, $mdUtil) {
634634
};
635635

636636
function postLink(scope, element, attr, ctrls) {
637-
var maxlength;
637+
var maxlength = parseInt(attr.mdMaxlength);
638+
if (isNaN(maxlength)) maxlength = -1;
638639
var ngModelCtrl = ctrls[0];
639640
var containerCtrl = ctrls[1];
640641
var charCountEl, errorsSpacer;
641642

643+
644+
ngModelCtrl.$validators['md-maxlength'] = function(modelValue, viewValue) {
645+
if (!angular.isNumber(maxlength) || maxlength < 0) {
646+
return true;
647+
}
648+
649+
// We always update the char count, when the modelValue has changed.
650+
// Using the $validators for triggering the update works very well.
651+
renderCharCount();
652+
653+
return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
654+
};
655+
642656
// Wait until the next tick to ensure that the input has setup the errors spacer where we will
643657
// append our counter
644658
$mdUtil.nextTick(function() {
@@ -663,23 +677,11 @@ function mdMaxlengthDirective($animate, $mdUtil) {
663677
$animate.leave(charCountEl);
664678
}
665679
});
666-
667-
ngModelCtrl.$validators['md-maxlength'] = function(modelValue, viewValue) {
668-
if (!angular.isNumber(maxlength) || maxlength < 0) {
669-
return true;
670-
}
671-
672-
// We always update the char count, when the modelValue has changed.
673-
// Using the $validators for triggering the update works very well.
674-
renderCharCount();
675-
676-
return ( modelValue || element.val() || viewValue || '' ).length <= maxlength;
677-
};
678680
});
679681

680682
function renderCharCount(value) {
681-
// If we have not been appended to the body yet; do not render
682-
if (!charCountEl.parent) {
683+
// If we have not been initialized or appended to the body yet; do not render
684+
if (!charCountEl || !charCountEl.parent) {
683685
return value;
684686
}
685687

src/components/input/input.spec.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,40 @@ describe('md-input-container directive', function() {
257257
return angular.element(el[0].querySelector('.md-char-counter'));
258258
}
259259

260+
it('should error with a constant and incorrect initial value', function() {
261+
var el = $compile(
262+
'<form name="form">' +
263+
' <md-input-container>' +
264+
' <input md-maxlength="2" ng-model="foo" name="foo">' +
265+
' </md-input-container>' +
266+
'</form>')(pageScope);
267+
268+
pageScope.$apply('foo = "ABCDEFGHIJ"');
269+
270+
// Flush any pending $mdUtil.nextTick calls
271+
$timeout.flush();
272+
273+
expect(pageScope.form.foo.$error['md-maxlength']).toBe(true);
274+
expect(getCharCounter(el).text()).toBe('10 / 2');
275+
});
276+
277+
it('should work with a constant and correct initial value', function() {
278+
var el = $compile(
279+
'<form name="form">' +
280+
' <md-input-container>' +
281+
' <input md-maxlength="5" ng-model="foo" name="foo">' +
282+
' </md-input-container>' +
283+
'</form>')(pageScope);
284+
285+
pageScope.$apply('foo = "abcde"');
286+
287+
// Flush any pending $mdUtil.nextTick calls
288+
$timeout.flush();
289+
290+
expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
291+
expect(getCharCounter(el).text()).toBe('5 / 5');
292+
});
293+
260294
it('should work with a constant', function() {
261295
var el = $compile(
262296
'<form name="form">' +
@@ -341,11 +375,10 @@ describe('md-input-container directive', function() {
341375
' </md-input-container>' +
342376
'</form>')(pageScope);
343377

344-
pageScope.$apply();
378+
pageScope.$apply('max = -1');
345379

346380
// Flush any pending $mdUtil.nextTick calls
347381
$timeout.flush();
348-
349382
expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
350383
expect(getCharCounter(el).length).toBe(0);
351384

0 commit comments

Comments
 (0)