This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
fix(ngModel): don’t clear the model when an external validator failed #9016
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,6 +214,15 @@ describe('NgModelController', function() { | |
expect(ctrl.$modelValue).toBeUndefined(); | ||
}); | ||
|
||
it('should not reset the model when the view is invalid due to an external validator', function() { | ||
ctrl.$setViewValue('aaaa'); | ||
expect(ctrl.$modelValue).toBe('aaaa'); | ||
|
||
ctrl.$setValidity('someExternalError', false); | ||
ctrl.$setViewValue('bbbb'); | ||
expect(ctrl.$modelValue).toBe('bbbb'); | ||
}); | ||
|
||
it('should not reset the view when the view is invalid', function() { | ||
// this test fails when the view changes the model and | ||
// then the model listener in ngModel picks up the change and | ||
|
@@ -302,6 +311,13 @@ describe('NgModelController', function() { | |
expect(ctrl.$error).toEqual({ high : true }); | ||
}); | ||
|
||
it('should not remove external validators when a parser failed', function() { | ||
ctrl.$parsers.push(function(v) { return undefined; }); | ||
ctrl.$setValidity('externalError', false); | ||
ctrl.$setViewValue('someValue'); | ||
expect(ctrl.$error).toEqual({ externalError : true, parse: true }); | ||
}); | ||
|
||
it('should remove all non-parse-related CSS classes from the form when a parser fails', | ||
inject(function($compile, $rootScope) { | ||
|
||
|
@@ -711,7 +727,7 @@ describe('NgModelController', function() { | |
expect(ctrl.$pending).toBeUndefined(); | ||
})); | ||
|
||
it('should clear and ignore all pending promises when the input values changes', inject(function($q) { | ||
it('should clear and ignore all pending promises when the model values changes', inject(function($q) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/changes/change |
||
ctrl.$validators.sync = function(value) { | ||
return true; | ||
}; | ||
|
@@ -775,6 +791,44 @@ describe('NgModelController', function() { | |
expect(isObject(ctrl.$pending)).toBe(false); | ||
})); | ||
|
||
it('should clear all errors from async validators if a parser fails', inject(function($q) { | ||
var failParser = false; | ||
ctrl.$parsers.push(function(value) { | ||
return failParser ? undefined : value; | ||
}); | ||
|
||
ctrl.$asyncValidators.async = function(value) { | ||
return $q.reject(); | ||
}; | ||
|
||
ctrl.$setViewValue('x..y..z'); | ||
expect(ctrl.$error).toEqual({async: true}); | ||
|
||
failParser = true; | ||
|
||
ctrl.$setViewValue('1..2..3'); | ||
expect(ctrl.$error).toEqual({parse: true}); | ||
})); | ||
|
||
it('should clear all errors from async validators if a sync validator fails', inject(function($q) { | ||
var failValidator = false; | ||
ctrl.$validators.sync = function(value) { | ||
return !failValidator; | ||
}; | ||
|
||
ctrl.$asyncValidators.async = function(value) { | ||
return $q.reject(); | ||
}; | ||
|
||
ctrl.$setViewValue('x..y..z'); | ||
expect(ctrl.$error).toEqual({async: true}); | ||
|
||
failValidator = true; | ||
|
||
ctrl.$setViewValue('1..2..3'); | ||
expect(ctrl.$error).toEqual({sync: true}); | ||
})); | ||
|
||
it('should re-evaluate the form validity state once the asynchronous promise has been delivered', | ||
inject(function($compile, $rootScope, $q) { | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@tbosch It'd be nice to have a reject handler here as well in case the previous step rejects/throws. Right now it seems pretty safe, but future additions could introduce cases where an unhandled rejection is possible.
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.
I will add
noop
explicitly, so that it is clear that we do want to do nothing when we have an error there. This makes sense asvalidationDone
also calls thedoneCallback
conditionally...