Skip to content

Commit

Permalink
Merge pull request #223 from nmccready/originalValueTracking
Browse files Browse the repository at this point in the history
Track originalViewValue
  • Loading branch information
Huei Tan authored Jul 21, 2016
2 parents f87a51c + 5d9085d commit f26019c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
7 changes: 6 additions & 1 deletion dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ angular.module('validation.directive', ['validation.provider']);
*/
var uid = ctrl.validationId = guid();

/**
* to have avalue to rollback to
*/
var originalViewValue = null;

/**
* Set initial validity to undefined if no boolean value is transmitted
Expand Down Expand Up @@ -687,7 +691,7 @@ angular.module('validation.directive', ['validation.provider']);
watch();

$timeout(function() {
ctrl.$setViewValue('');
ctrl.$setViewValue(originalViewValue);
ctrl.$setPristine();
ctrl.$setValidity(ctrl.$name, undefined);
ctrl.$render();
Expand Down Expand Up @@ -783,6 +787,7 @@ angular.module('validation.directive', ['validation.provider']);
*/
if (ctrl.$pristine && ctrl.$viewValue) {
// has value when initial
originalViewValue = ctrl.$viewValue || '';
ctrl.$setViewValue(ctrl.$viewValue);
} else if (ctrl.$pristine) {
// Don't validate form when the input is clean(pristine)
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-validation.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/validator.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@
*/
var uid = ctrl.validationId = guid();

/**
* to have a value to rollback to
*/
var originalViewValue = null;

/**
* Set initial validity to undefined if no boolean value is transmitted
Expand Down Expand Up @@ -309,7 +313,7 @@
watch();

$timeout(function() {
ctrl.$setViewValue('');
ctrl.$setViewValue(originalViewValue);
ctrl.$setPristine();
ctrl.$setValidity(ctrl.$name, undefined);
ctrl.$render();
Expand Down Expand Up @@ -405,6 +409,7 @@
*/
if (ctrl.$pristine && ctrl.$viewValue) {
// has value when initial
originalViewValue = ctrl.$viewValue || '';
ctrl.$setViewValue(ctrl.$viewValue);
} else if (ctrl.$pristine) {
// Don't validate form when the input is clean(pristine)
Expand Down

0 comments on commit f26019c

Please sign in to comment.