-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(input): make md-maxlength validation happen on initialization #11150
Conversation
Same as #11149 (comment), Please add "Closes #10320" to the footer of your commit message as detailed in the commit message guidelines. Just adding it the PR description is not enough as that won't trigger the issue to be closed on merge or show up in the changelog properly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Thanks for adding the tests!
I just had a couple of minor questions.
src/components/input/input.js
Outdated
@@ -634,11 +634,25 @@ function mdMaxlengthDirective($animate, $mdUtil) { | |||
}; | |||
|
|||
function postLink(scope, element, attr, ctrls) { | |||
var maxlength; | |||
var maxlength = Number.parseInt(attr.mdMaxlength); | |||
if (isNaN(maxlength)) maxlength = -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use !angular.isNumber(maxlength)
to be consistent with line 645, or is there a reason that isNaN
better here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's not equivalent here, because angular.isNumber(NaN) returns true but in this case, we do not want to keep NaN.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I thought that angular.isNumber()
was smarter than it actually is.
src/components/input/input.js
Outdated
}); | ||
|
||
function renderCharCount(value) { | ||
// If we have not been appended to the body yet; do not render | ||
if (!charCountEl.parent) { | ||
if (!charCountEl || !charCountEl.parent) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is ||
correct here? Should this be &&
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now have to check that charCountEl is not undefined or charCountEl.text will fail in the following lines in some cases (because charCountEl is initialized at nextTick and renderCharCount can now be called before nextTick, from validator).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, can you please update the comment as well?
Btw, amending commits is the best way to keep your changes in a single commit when updates are needed. Then force push to your branch is the best way to sync those updates to the branch. |
759a501
to
c31fffd
Compare
Looks great! Thank you for your patience. I've sent this over to the caretaker to have presubmit tests run. |
This is currently triggering some presubmit failures that appear to be flakes (i.e. from flaky tests, not actual real failures). This is being looked at internally by Google. No work is needed on your side at this time. |
I re-ran the failing build steps in Travis and everything is green now 😃 |
This fails google presubmit because |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IE11 support is needed, then we can merge this. Thanks!
src/components/input/input.js
Outdated
@@ -634,11 +634,25 @@ function mdMaxlengthDirective($animate, $mdUtil) { | |||
}; | |||
|
|||
function postLink(scope, element, attr, ctrls) { | |||
var maxlength; | |||
var maxlength = Number.parseInt(attr.mdMaxlength); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As pointed out by Jeremy, Number.parseInt
is not supported in IE11. So we'll need to use something different here.
It looks like the global parseInt should work?
MDN also points out that Number.parseInt has no IE support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used the global parseInt instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
PR Checklist
Please check that your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
When an input has an md-maxlength attribute and an initial value longer than this maxlength, no error is triggered until the input value is changed.
Issue Number: #10320
What is the new behavior?
When an input has an md-maxlength attribute and an initial value longer than this maxlength, an error is triggered at initialization.
Note that the error message is not shown at initialization but only when the focus leave the input (without content changes), which seems to be the correct behavior (in a similar way, for 'required' input with empty initial value, no message is displayed at initialization).
Does this PR introduce a breaking change?
Other information
Closes #10320