-
Notifications
You must be signed in to change notification settings - Fork 27.5k
min/max input validation is against $modelValue, not $viewValue #12761
Comments
$viewValue is string, $modelValue is of the model type (number in your case). |
I tend to agree that the HTML5 validators should work against the input values. This might break people's apps however. Might be something for 1.5 |
Sorry for the delay. It definitely make sense to validate against $viewValue. From the ideological side in most of the cases validation messages should notify about errors of value that is currently visible on the screen. Also in some cases user might require validation for $modelValue, so validation target value (either $modelValue or $viewValue or both?) could be configurable in ng-model-options. And this way we could have backwards compatibility with older versions of angular. By the way i took a peek into existing required/minlength/maxlength/pattern validators -- all of them validates against $viewValue.. |
Hello, **UPDATE |
Any updates to this? I am using angular 1.5.8 and this problem still exists. |
@Narretz can we discuss whether this should be considered "workaround-able"? |
It's work-aroundable as you can copy the validation code and apply it to the $viewValue instead of the $modelValue. However, I think for the sake of consistency we should change it to validate the $viewValue, and then the same applies for devs that want to validate the $modelValue. For reference, the validation atm is as follows:
So it is inconsistent, but there's a tendency to validate the viewValue. While it might look like for date and number, it necessary to validate the modelValue because it has been converted to an Object, in range, we convert the viewValue to a Number for the step validation, so we can definitely transform the viewValue for the other validators as well. Another idea I had was to make it configurable via ngModelOptions if $modelValue / $viewValue gets validated, but this might become too complex. |
This brings the validation in line with HTML5 validation and all other validators. Fixes angular#12761 BREAKING CHANGE ...
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); }; } } ```
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 #12761 Closes #16325 BREAKING CHANGE `input[type=number]` with `ngModel` now validates the input for the `max`/`min` restriction against the `ngModelController.$viewValue` instead of 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 from a custom directive, as seen in the following example directive definition object: ``` { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { var maxValidator = ctrl.$validators.max; ctrk.$validators.max = function(modelValue, viewValue) { return maxValidator(modelValue, modelValue); }; } } ```
Hi,
I am using ngModelController.$parsers/$formatters pipeline to do the number conversion and default min/max validators. My problem is that validation is done against $modelValue. So in my case when i enter value that is still valid in view validators reports it is invalid in model.
I want to propose to do default (min/max/etc.) validation on $viewValue. This way input limitations applied by browser would match validators rules.
I've made plunker to ilustrate: http://plnkr.co/edit/1IOnf9HYjgHhvpD0QsI3?p=preview
Cheers,
Edvinas
The text was updated successfully, but these errors were encountered: