Skip to content

Commit 4dcdb68

Browse files
author
Huei Tan
committed
Merge pull request #140 from huei90/actualValue
The actual value from the control's view (ctrl.$viewValue instead of …
2 parents b268827 + 2db2ad7 commit 4dcdb68

File tree

6 files changed

+148
-25
lines changed

6 files changed

+148
-25
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module.exports = function(grunt) {
4343
}
4444
},
4545
jsbeautifier: {
46-
files: ['*.js', 'src/**/*.js'],
46+
files: ['*.js', 'src/**/*.js', 'test/unit/*.js'],
4747
options: {}
4848
},
4949
jshint: {

dist/angular-validation.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@
550550
if (attrs.validMethod === 'submit') {
551551
watch(); // clear previous scope.$watch
552552
watch = scope.$watch('model', function(value, oldValue) {
553+
value = ctrl.$viewValue;
553554

554555
// don't watch when init
555556
if (value === oldValue) {
@@ -610,6 +611,7 @@
610611
* This is the default method
611612
*/
612613
scope.$watch('model', function(value) {
614+
value = ctrl.$viewValue;
613615
/**
614616
* dirty, pristine, viewValue control here
615617
*/

dist/angular-validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/directive.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
if (attrs.validMethod === 'submit') {
259259
watch(); // clear previous scope.$watch
260260
watch = scope.$watch('model', function(value, oldValue) {
261+
value = ctrl.$viewValue;
261262

262263
// don't watch when init
263264
if (value === oldValue) {
@@ -318,6 +319,7 @@
318319
* This is the default method
319320
*/
320321
scope.$watch('model', function(value) {
322+
value = ctrl.$viewValue;
321323
/**
322324
* dirty, pristine, viewValue control here
323325
*/

test/unit/actualValue.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict';
2+
3+
/* jasmine specs for provider go here */
4+
5+
describe('provider', function() {
6+
7+
var $rootScope,
8+
$compile,
9+
$scope,
10+
$timeout,
11+
element,
12+
validationProvider,
13+
myApp;
14+
15+
beforeEach(function() {
16+
myApp = angular.module('myApp', ['validation', 'validation.rule'])
17+
.config(function($validationProvider) {
18+
validationProvider = $validationProvider;
19+
});
20+
21+
return myApp;
22+
});
23+
24+
beforeEach(module('myApp'));
25+
26+
beforeEach(inject(function($injector) {
27+
$rootScope = $injector.get('$rootScope');
28+
$compile = $injector.get('$compile');
29+
$scope = $rootScope.$new();
30+
$timeout = $injector.get('$timeout');
31+
32+
element = $compile('<form name="Form"><input type="number" name="numberWatch" ng-model="number" validator="number,maxlength=4" email-error-message="Error Number" email-success-message="Good Number"/></form>')($scope);
33+
angular.element(document.body).append(element);
34+
$scope.$digest();
35+
}));
36+
37+
it('set value to 123', inject(function() {
38+
39+
var submitSpy = jasmine.createSpy('submitSpy'),
40+
successSpy = jasmine.createSpy('successSpy'),
41+
errorSpy = jasmine.createSpy('errorSpy');
42+
43+
$scope.$apply(function() {
44+
$scope.number = 123;
45+
});
46+
47+
$scope.$on('numberWatchsubmit-' + $scope.Form.numberWatch.validationId, function() {
48+
submitSpy();
49+
});
50+
51+
validationProvider.validate($scope.Form)
52+
.success(function() {
53+
successSpy();
54+
})
55+
.error(function() {
56+
errorSpy();
57+
});
58+
59+
$timeout.flush();
60+
expect(submitSpy).toHaveBeenCalled();
61+
expect(successSpy).toHaveBeenCalled();
62+
expect(errorSpy).not.toHaveBeenCalled();
63+
64+
}));
65+
66+
it('set value to 1234567', inject(function() {
67+
68+
var submitSpy = jasmine.createSpy('submitSpy'),
69+
successSpy = jasmine.createSpy('successSpy'),
70+
errorSpy = jasmine.createSpy('errorSpy');
71+
72+
$scope.$apply(function() {
73+
$scope.number = 1234567;
74+
});
75+
76+
$scope.$on('numberWatchsubmit-' + $scope.Form.numberWatch.validationId, function() {
77+
submitSpy();
78+
});
79+
80+
validationProvider.validate($scope.Form)
81+
.success(function() {
82+
successSpy();
83+
})
84+
.error(function() {
85+
errorSpy();
86+
});
87+
88+
$timeout.flush();
89+
expect(submitSpy).toHaveBeenCalled();
90+
expect(successSpy).not.toHaveBeenCalled();
91+
expect(errorSpy).toHaveBeenCalled();
92+
93+
}));
94+
95+
it('set value to "ABC"', inject(function() {
96+
97+
var submitSpy = jasmine.createSpy('submitSpy'),
98+
successSpy = jasmine.createSpy('successSpy'),
99+
errorSpy = jasmine.createSpy('errorSpy');
100+
101+
// expect error because we are using <input type="number"/> not type="text"
102+
try {
103+
$scope.$apply(function() {
104+
$scope.number = 'ABC';
105+
});
106+
} catch (e) {
107+
errorSpy();
108+
}
109+
110+
$scope.$on('numberWatchsubmit-' + $scope.Form.numberWatch.validationId, function() {
111+
submitSpy();
112+
});
113+
114+
expect(submitSpy).not.toHaveBeenCalled();
115+
expect(successSpy).not.toHaveBeenCalled();
116+
expect(errorSpy).toHaveBeenCalled();
117+
118+
}));
119+
});

0 commit comments

Comments
 (0)