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

feat(mdMaxlength): support use with required and ng-trim #11136

Merged
merged 2 commits into from
May 4, 2018

Conversation

Splaktar
Copy link
Member

@Splaktar Splaktar commented Feb 27, 2018

PR Checklist

Please check that your PR fulfills the following requirements:

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Infrastructure changes
[ ] Other... Please describe:

What is the current behavior?

When md-maxlength is used on an input that has the required attribute set, the required validation will accept blank spaces in the input as valid by default. Trying to override this with ng-trim="true" is ignored.

Issue Number:
Fixes #10082
Fixes #10216
Relates to #5321

What is the new behavior?

By default, blank spaces in required inputs cause an invalid state. If this is undesirable, it can be overridden by adding ng-trim="false" to the input.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

This is based on an abandoned PR (#10083).

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this State. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@googlebot googlebot added the cla: no PR author needs to sign Google's CLA: https://opensource.google.com/docs/cla/ label Feb 27, 2018
@Splaktar Splaktar changed the title feat(mdMaxlength): support use with required and ng-trim WIP: feat(mdMaxlength): support use with required and ng-trim Feb 27, 2018
@Splaktar Splaktar added cla: yes PR author has signed Google's CLA: https://opensource.google.com/docs/cla/ in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs P5: nice to have These issues will not be fixed without community contributions. and removed cla: no PR author needs to sign Google's CLA: https://opensource.google.com/docs/cla/ labels Feb 27, 2018
@Splaktar Splaktar added this to the 1.1.9 milestone Feb 27, 2018
@Splaktar
Copy link
Member Author

CLA consent here: #10083 (comment)

@Splaktar Splaktar self-assigned this Feb 27, 2018
@Splaktar
Copy link
Member Author

I can't get the test to pass which is supposed to verify this functionality. In manual testing (demos), I can clearly see the functionality working as expected. But I can't get the required error to trigger in the test.

@Splaktar Splaktar modified the milestones: 1.1.9, 1.1.8 Mar 13, 2018
@Splaktar Splaktar removed the in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs label Mar 13, 2018
@Splaktar Splaktar changed the title WIP: feat(mdMaxlength): support use with required and ng-trim feat(mdMaxlength): support use with required and ng-trim Mar 13, 2018
@Splaktar Splaktar added the needs: review This PR is waiting on review from the team label Mar 13, 2018
@Splaktar
Copy link
Member Author

Splaktar commented Mar 13, 2018

The previous test issues have been resolved. Now the only failing tests have to do with AngularJS 1.5.x...

@Splaktar Splaktar modified the milestones: 1.1.8, 1.1.9 Mar 13, 2018
@Splaktar Splaktar removed the request for review from jelbourn March 13, 2018 08:52
@Splaktar Splaktar added the in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs label Mar 13, 2018
Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Splaktar Splaktar removed the needs: review This PR is waiting on review from the team label Mar 13, 2018
@Splaktar
Copy link
Member Author

Thanks a ton @gkalpak! That's exactly the kind of feedback that I was looking for! I'll re-visit and make changes.

@gkalpak
Copy link
Member

gkalpak commented Mar 13, 2018

👍 Feel free to ping me for another review 😃

Adding ng-trim="false" to an input will cause the 'required' state to be fulfilled by empty spaces, essentially allowing a user to put in no content and pass required validation.  Allowing angular to trim, and trimming manually in the renderCharCount function works better (IMO).
@googlebot googlebot added cla: no PR author needs to sign Google's CLA: https://opensource.google.com/docs/cla/ and removed cla: yes PR author has signed Google's CLA: https://opensource.google.com/docs/cla/ labels Apr 19, 2018
@Splaktar Splaktar removed the cla: no PR author needs to sign Google's CLA: https://opensource.google.com/docs/cla/ label Apr 19, 2018
@Splaktar
Copy link
Member Author

@gkalpak can you please take another look?

viewValue did not have trim already applied in my debugging.
I believe that I was able to properly override the ngModelCtrl.$isEmpty() function instead of the required validator.
The last few checks that didn't work for 0 should be fixed.

@Splaktar Splaktar added the needs: review This PR is waiting on review from the team label Apr 19, 2018
@Splaktar Splaktar modified the milestones: 1.1.9, 1.1.10 Apr 24, 2018
Copy link
Member

@gkalpak gkalpak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of nits.
LGTM otherwise 👍

elementVal = '';
}
elementVal = ngTrim && !isPasswordInput && angular.isString(elementVal) ? elementVal.trim() : elementVal;
return String(elementVal).length === 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: You can make a re-usable function for calculating the length of a value (which is used in the validator, $isEmpty() and renderCharCount()):

function caclulateLength(value) {
  if (value == null) {
    value = '';
  } else {
    value = ngTrim && !isPasswordInput && angular.isString(value) ? value.trim() : value;
  }
  return String(value).length;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a feeling that this was needed. Thanks for confirming it. Much cleaner 😃

expect(pageScope.form.foo.$error['md-maxlength']).toBeFalsy();
});

it('should not trim spaces for required password inputs by default', function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: This is not just "by default". It is the only posible behavior (i.e. it is not possible to override it).


pageScope.$apply('foo = " "');
expect(pageScope.form.foo.$error['required']).toBeFalsy();
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOC, why not verify pageScope.form.foo.$error['md-maxlength'] too? (Possibly using foo = " " as well to make it truthy.)

no longer force ng-trim="false"
trim the value by default or based on the ng-trim attribute
don't trim password inputs to align with AngularJS behavior

Fixes angular#10082
@googlebot googlebot added the cla: no PR author needs to sign Google's CLA: https://opensource.google.com/docs/cla/ label Apr 26, 2018
@Splaktar Splaktar added pr: merge ready This PR is ready for a caretaker to review and removed cla: no PR author needs to sign Google's CLA: https://opensource.google.com/docs/cla/ in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs needs: review This PR is waiting on review from the team labels Apr 26, 2018
@Splaktar Splaktar assigned andrewseguin and unassigned Splaktar Apr 26, 2018
@josephperrott josephperrott added the cla: yes PR author has signed Google's CLA: https://opensource.google.com/docs/cla/ label May 4, 2018
@googlebot
Copy link

A Googler has manually verified that the CLAs look good.

(Googler, please make sure the reason for overriding the CLA status is clearly documented in these comments.)

@josephperrott
Copy link
Member

Setting CLA as yes after confirming both listed PR authors have signed CLA.

@josephperrott josephperrott merged commit db1a85d into angular:master May 4, 2018
@Splaktar Splaktar deleted the mdMaxlengthNgTrim branch May 4, 2018 23:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has signed Google's CLA: https://opensource.google.com/docs/cla/ P5: nice to have These issues will not be fixed without community contributions. pr: merge ready This PR is ready for a caretaker to review type: enhancement
Projects
None yet
7 participants