Skip to content

Commit 071f735

Browse files
author
Huei Tan
committed
Merge pull request #168 from lucax88x/master
Added a global valid method
2 parents 3aa4380 + ab18c9f commit 071f735

6 files changed

Lines changed: 79 additions & 11 deletions

File tree

API.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ You can also add a custom validation message by using `message-id` attribute. It
120120
<!-- Clean, right ? -->
121121
```
122122

123+
### **Select a global validation method** `watch blur submit submit-only`**
124+
125+
`validationProvider.setValidMethod('submit')`
126+
123127
### **Setup a new Validation `setExpression()` `setDefaultMsg()` with `RegExp` or `Function` in config phase**
124128
<a name="custom-function-huei"></a>
125129

dist/angular-validation.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
angular.module('validation.provider', []);
44
angular.module('validation.directive', ['validation.provider']);
55
}).call(this);
6-
6+
77
(function() {
88
angular
99
.module('validation.provider')
@@ -35,6 +35,12 @@
3535
*/
3636
var expression = {};
3737

38+
/**
39+
* default valid method
40+
* @type {{}}
41+
*/
42+
var validMethod = null;
43+
3844
/**
3945
* default error, success message
4046
* @type {{}}
@@ -79,6 +85,23 @@
7985
return defaultMsg[msg];
8086
};
8187

88+
/**
89+
* allow user to set the global valid method
90+
* @param v
91+
* @returns {*}
92+
*/
93+
this.setValidMethod = function(v) {
94+
validMethod = v;
95+
};
96+
97+
/**
98+
* Get the valid method
99+
* @returns {*}
100+
*/
101+
this.getValidMethod = function() {
102+
return validMethod;
103+
};
104+
82105
/**
83106
* Override the errorHTML function
84107
* @param func
@@ -247,6 +270,8 @@
247270
this.$get = ['$injector', function($injector) {
248271
setup($injector);
249272
return {
273+
setValidMethod: this.setValidMethod,
274+
getValidMethod: this.getValidMethod,
250275
setErrorHTML: this.setErrorHTML,
251276
getErrorHTML: this.getErrorHTML,
252277
setSuccessHTML: this.setSuccessHTML,
@@ -266,7 +291,7 @@
266291
}];
267292
}
268293
}).call(this);
269-
294+
270295
(function() {
271296
angular
272297
.module('validation.directive')
@@ -290,7 +315,7 @@
290315
}
291316
Reset.$inject = ['$injector'];
292317
}).call(this);
293-
318+
294319
(function() {
295320
angular
296321
.module('validation.directive')
@@ -321,7 +346,7 @@
321346
}
322347
Submit.$inject = ['$injector'];
323348
}).call(this);
324-
349+
325350
(function() {
326351
angular
327352
.module('validation.directive')
@@ -556,6 +581,9 @@
556581
* Check validator
557582
*/
558583

584+
585+
var validMethod = (angular.isUndefined(attrs.validMethod)) ? $validationProvider.getValidMethod() : attrs.validMethod;
586+
559587
/**
560588
* Click submit form, check the validity when submit
561589
*/
@@ -565,7 +593,7 @@
565593

566594
isValid = checkValidation(scope, element, attrs, ctrl, validation, value);
567595

568-
if (attrs.validMethod === 'submit') {
596+
if (validMethod === 'submit') {
569597
// clear previous scope.$watch
570598
watch();
571599
watch = scope.$watch(function() {
@@ -606,7 +634,7 @@
606634
/**
607635
* Validate blur method
608636
*/
609-
if (attrs.validMethod === 'blur') {
637+
if (validMethod === 'blur') {
610638
element.bind('blur', function() {
611639
var value = scope.$eval(attrs.ngModel);
612640
scope.$apply(function() {
@@ -620,7 +648,7 @@
620648
/**
621649
* Validate submit & submit-only method
622650
*/
623-
if (attrs.validMethod === 'submit' || attrs.validMethod === 'submit-only') {
651+
if (validMethod === 'submit' || validMethod === 'submit-only') {
624652
return;
625653
}
626654

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/provider.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
*/
3030
var expression = {};
3131

32+
/**
33+
* default valid method
34+
* @type {{}}
35+
*/
36+
var validMethod = null;
37+
3238
/**
3339
* default error, success message
3440
* @type {{}}
@@ -73,6 +79,23 @@
7379
return defaultMsg[msg];
7480
};
7581

82+
/**
83+
* allow user to set the global valid method
84+
* @param v
85+
* @returns {*}
86+
*/
87+
this.setValidMethod = function(v) {
88+
validMethod = v;
89+
};
90+
91+
/**
92+
* Get the valid method
93+
* @returns {*}
94+
*/
95+
this.getValidMethod = function() {
96+
return validMethod;
97+
};
98+
7699
/**
77100
* Override the errorHTML function
78101
* @param func
@@ -241,6 +264,8 @@
241264
this.$get = ['$injector', function($injector) {
242265
setup($injector);
243266
return {
267+
setValidMethod: this.setValidMethod,
268+
getValidMethod: this.getValidMethod,
244269
setErrorHTML: this.setErrorHTML,
245270
getErrorHTML: this.getErrorHTML,
246271
setSuccessHTML: this.setSuccessHTML,

src/validator.directive.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@
232232
* Check validator
233233
*/
234234

235+
236+
var validMethod = (angular.isUndefined(attrs.validMethod)) ? $validationProvider.getValidMethod() : attrs.validMethod;
237+
235238
/**
236239
* Click submit form, check the validity when submit
237240
*/
@@ -241,7 +244,7 @@
241244

242245
isValid = checkValidation(scope, element, attrs, ctrl, validation, value);
243246

244-
if (attrs.validMethod === 'submit') {
247+
if (validMethod === 'submit') {
245248
// clear previous scope.$watch
246249
watch();
247250
watch = scope.$watch(function() {
@@ -282,7 +285,7 @@
282285
/**
283286
* Validate blur method
284287
*/
285-
if (attrs.validMethod === 'blur') {
288+
if (validMethod === 'blur') {
286289
element.bind('blur', function() {
287290
var value = scope.$eval(attrs.ngModel);
288291
scope.$apply(function() {
@@ -296,7 +299,7 @@
296299
/**
297300
* Validate submit & submit-only method
298301
*/
299-
if (attrs.validMethod === 'submit' || attrs.validMethod === 'submit-only') {
302+
if (validMethod === 'submit' || validMethod === 'submit-only') {
300303
return;
301304
}
302305

test/unit/providerSpec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,12 @@ describe('provider', function() {
160160
expect(successSpy2).not.toHaveBeenCalled();
161161
expect(errorSpy2).toHaveBeenCalled();
162162
}));
163+
164+
it('set/get validMethod', inject(function() {
165+
expect(validationProvider.getValidMethod()).toEqual(null);
166+
167+
validationProvider.setValidMethod('submit');
168+
169+
expect(validationProvider.getValidMethod()).toEqual('submit');
170+
}));
163171
});

0 commit comments

Comments
 (0)