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

fix(input[number]): validate min/max against viewValue #16325

Merged
merged 1 commit into from
Nov 17, 2017

Conversation

Narretz
Copy link
Contributor

@Narretz Narretz commented Nov 10, 2017

This brings the validation in line with HTML5 validation and all other
validators.

Fixes #12761

BREAKING CHANGE

....

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix / normalization

What is the current behavior? (You can also link to an open issue here)
See #12761

What is the new behavior (if this is a feature change)?
validation of min / max against viewValue

Does this PR introduce a breaking change?
Yes

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Fix/Feature: Docs have been added/updated
  • Fix/Feature: Tests have been added; existing tests pass

Other information:

I believe the BC is moderate enough to be included in 1.7

To switch the validation to the modelValue, it's as easy as adding this into a directive:

var minValidator = ngModelCtrl.$validators.max;

        ngModelCtrl.$validators.max = function(modelValue, viewValue) {
          return minValidator(modelValue, modelValue);
        };

I could even publish this is as a configurable third party directive that allows you to do this for any validator.

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.

LGTM (except for the missing "breaking change" notice on the commit message).

return value + 5;
});

helper.changeInputValueTo('9');
Copy link
Member

Choose a reason for hiding this comment

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

Why not 10 (for symmetry with min)? 😢

@@ -2465,6 +2465,21 @@ describe('input', function() {
expect($rootScope.form.alias.$error.min).toBeFalsy();
});


it('should validate against the viewValue', function() {
var inputElm = helper.compileInput('<input type="number" num-parse ng-model="value" name="alias" min="10" />');
Copy link
Member

Choose a reason for hiding this comment

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

num-parse doesn't appear to be used anywhere

helper.changeInputValueTo('10');
expect(inputElm).toBeValid();
expect($rootScope.value).toBe(5);
expect($rootScope.form.alias.$error.min).toBeFalsy();
Copy link
Member

Choose a reason for hiding this comment

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

Should we also have tests that check that we do get an error if the viewValue is bad but the modelValue is good?

Copy link
Member

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

LGTM

  • we should add tests for bad viewValue -> good modelValue
  • we need a BC notice that includes the example directive for reverting the behaviour.

This brings the validation in line with HTML5 validation, i.e. what the user has entered
is validated, and not a possibly transformed value.

Fixes angular#12761

BREAKING CHANGE

`input[type=number]` with `ngModel` now validates the input for `max`/`min` against
the `ngModelController.$viewValue` instead of the against the `ngModelController.$modelValue`.

This affects apps that use `$parsers` or `$formatters` to transform the input / model value.

IF you rely on the $modelValue validation, you can overwrite the `min`/`max` validator, as in the
following example directive defintion object:

```
{
  restrict: 'A',
  require: 'ngModel',
  link: function(scope, element, attrs, ctrl) {
    var minValidator = ctrl.$validators.max;

    ctrk.$validators.max = function(modelValue, viewValue) {
      return minValidator(modelValue, modelValue);
    };
  }
}
```
@Narretz
Copy link
Contributor Author

Narretz commented Nov 14, 2017

Updated with your suggestions!

Copy link
Member

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

Did you mean to capitalize "IF" in the commit message?

@Narretz Narretz merged commit aa3f951 into angular:master Nov 17, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

min/max input validation is against $modelValue, not $viewValue
4 participants