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

Commit

Permalink
refactor(ngModel): get rid of revalidate
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shahata authored and petebacondarwin committed Sep 3, 2014
1 parent 0f806d9 commit c3064f7
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/ng/directive/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,6 @@ function baseInputType(scope, element, attr, ctrl, $sniffer, $browser) {
var validity = element.prop(VALIDITY_STATE_PROPERTY);
var placeholder = element[0].placeholder, noevent = {};
var type = lowercase(element[0].type);
ctrl.$$validityState = validity;

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

// If a control is suffering from bad input, browsers discard its value, so it may be
// necessary to revalidate even if the control's value is the same empty value twice in
// a row.
var revalidate = validity && ctrl.$$hasNativeValidators;
if (ctrl.$viewValue !== value || (value === '' && revalidate)) {
// If a control is suffering from bad input (due to native validators), browsers discard its
// value, so it may be necessary to revalidate (by calling $setViewValue again) even if the
// control's value is the same empty value twice in a row.
if (ctrl.$viewValue !== value || (value === '' && ctrl.$$hasNativeValidators)) {
if (scope.$root.$$phase) {
ctrl.$setViewValue(value, event, revalidate);
ctrl.$setViewValue(value, event);
} else {
scope.$apply(function() {
ctrl.$setViewValue(value, event, revalidate);
ctrl.$setViewValue(value, event);
});
}
}
Expand Down Expand Up @@ -1980,11 +1978,15 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
* event defined in `ng-model-options`. this method is rarely needed as `NgModelController`
* usually handles calling this in response to input events.
*/
this.$commitViewValue = function(revalidate) {
this.$commitViewValue = function() {
var viewValue = ctrl.$viewValue;

$timeout.cancel(pendingDebounce);
if (!revalidate && ctrl.$$lastCommittedViewValue === viewValue) {

// If the view value has not changed then we should just exit, except in the case where there is
// a native validator on the element. In this case the validation state may have changed even though
// the viewValue has stayed empty.
if (ctrl.$$lastCommittedViewValue === viewValue && (viewValue !== '' || !ctrl.$$hasNativeValidators)) {
return;
}
ctrl.$$lastCommittedViewValue = viewValue;
Expand Down Expand Up @@ -2080,14 +2082,14 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
* @param {string} value Value from the view.
* @param {string} trigger Event that triggered the update.
*/
this.$setViewValue = function(value, trigger, revalidate) {
this.$setViewValue = function(value, trigger) {
ctrl.$viewValue = value;
if (!ctrl.$options || ctrl.$options.updateOnDefault) {
ctrl.$$debounceViewValueCommit(trigger, revalidate);
ctrl.$$debounceViewValueCommit(trigger);
}
};

this.$$debounceViewValueCommit = function(trigger, revalidate) {
this.$$debounceViewValueCommit = function(trigger) {
var debounceDelay = 0,
options = ctrl.$options,
debounce;
Expand All @@ -2106,10 +2108,10 @@ var NgModelController = ['$scope', '$exceptionHandler', '$attrs', '$element', '$
$timeout.cancel(pendingDebounce);
if (debounceDelay) {
pendingDebounce = $timeout(function() {
ctrl.$commitViewValue(revalidate);
ctrl.$commitViewValue();
}, debounceDelay);
} else {
ctrl.$commitViewValue(revalidate);
ctrl.$commitViewValue();
}
};

Expand Down

0 comments on commit c3064f7

Please sign in to comment.