-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(input[number]): validate min/max against viewValue #16325
Conversation
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 (except for the missing "breaking change" notice on the commit message).
test/ng/directive/inputSpec.js
Outdated
return value + 5; | ||
}); | ||
|
||
helper.changeInputValueTo('9'); |
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.
Why not 10
(for symmetry with min
)? 😢
test/ng/directive/inputSpec.js
Outdated
@@ -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" />'); |
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.
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(); |
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 we also have tests that check that we do get an error if the viewValue
is bad but the modelValue
is good?
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
- 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); }; } } ```
0a3ddfc
to
74d96b9
Compare
Updated with your suggestions! |
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.
Did you mean to capitalize "IF" in the commit message?
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
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:
I could even publish this is as a configurable third party directive that allows you to do this for any validator.