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

Commit c3064f7

Browse files
shahatapetebacondarwin
authored andcommitted
refactor(ngModel): get rid of revalidate
Since the validation was refactored we can now work out inside `$commitViewValue()` whether to ignore validation by looking at whether the input has native validators. Closes #8856
1 parent 0f806d9 commit c3064f7

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/ng/directive/input.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,6 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
914914
var validity = element.prop(VALIDITY_STATE_PROPERTY);
915915
var placeholder = element[0].placeholder, noevent = {};
916916
var type = lowercase(element[0].type);
917-
ctrl.$$validityState = validity;
918917

919918
// In composition mode, users are still inputing intermediate text buffer,
920919
// hold the listener until composition is done.
@@ -953,16 +952,15 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
953952
value = trim(value);
954953
}
955954

956-
// If a control is suffering from bad input, browsers discard its value, so it may be
957-
// necessary to revalidate even if the control's value is the same empty value twice in
958-
// a row.
959-
var revalidate = validity && ctrl.$$hasNativeValidators;
960-
if (ctrl.$viewValue !== value || (value === '' && revalidate)) {
955+
// If a control is suffering from bad input (due to native validators), browsers discard its
956+
// value, so it may be necessary to revalidate (by calling $setViewValue again) even if the
957+
// control's value is the same empty value twice in a row.
958+
if (ctrl.$viewValue !== value || (value === '' && ctrl.$$hasNativeValidators)) {
961959
if (scope.$root.$$phase) {
962-
ctrl.$setViewValue(value, event, revalidate);
960+
ctrl.$setViewValue(value, event);
963961
} else {
964962
scope.$apply(function() {
965-
ctrl.$setViewValue(value, event, revalidate);
963+
ctrl.$setViewValue(value, event);
966964
});
967965
}
968966
}
@@ -1980,11 +1978,15 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
19801978
* event defined in `ng-model-options`. this method is rarely needed as `NgModelController`
19811979
* usually handles calling this in response to input events.
19821980
*/
1983-
this.$commitViewValue = function(revalidate) {
1981+
this.$commitViewValue = function() {
19841982
var viewValue = ctrl.$viewValue;
19851983

19861984
$timeout.cancel(pendingDebounce);
1987-
if (!revalidate && ctrl.$$lastCommittedViewValue === viewValue) {
1985+
1986+
// If the view value has not changed then we should just exit, except in the case where there is
1987+
// a native validator on the element. In this case the validation state may have changed even though
1988+
// the viewValue has stayed empty.
1989+
if (ctrl.$$lastCommittedViewValue === viewValue && (viewValue !== '' || !ctrl.$$hasNativeValidators)) {
19881990
return;
19891991
}
19901992
ctrl.$$lastCommittedViewValue = viewValue;
@@ -2080,14 +2082,14 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
20802082
* @param {string} value Value from the view.
20812083
* @param {string} trigger Event that triggered the update.
20822084
*/
2083-
this.$setViewValue = function(value, trigger, revalidate) {
2085+
this.$setViewValue = function(value, trigger) {
20842086
ctrl.$viewValue = value;
20852087
if (!ctrl.$options || ctrl.$options.updateOnDefault) {
2086-
ctrl.$$debounceViewValueCommit(trigger, revalidate);
2088+
ctrl.$$debounceViewValueCommit(trigger);
20872089
}
20882090
};
20892091

2090-
this.$$debounceViewValueCommit = function(trigger, revalidate) {
2092+
this.$$debounceViewValueCommit = function(trigger) {
20912093
var debounceDelay = 0,
20922094
options = ctrl.$options,
20932095
debounce;
@@ -2106,10 +2108,10 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
21062108
$timeout.cancel(pendingDebounce);
21072109
if (debounceDelay) {
21082110
pendingDebounce = $timeout(function() {
2109-
ctrl.$commitViewValue(revalidate);
2111+
ctrl.$commitViewValue();
21102112
}, debounceDelay);
21112113
} else {
2112-
ctrl.$commitViewValue(revalidate);
2114+
ctrl.$commitViewValue();
21132115
}
21142116
};
21152117

0 commit comments

Comments
 (0)