Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a global valid method #168

Merged
merged 4 commits into from
Dec 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ You can also add a custom validation message by using `message-id` attribute. It
<!-- Clean, right ? -->
```

### **Select a global validation method** `watch blur submit submit-only`**

`validationProvider.setValidMethod('submit')`

### **Setup a new Validation `setExpression()` `setDefaultMsg()` with `RegExp` or `Function` in config phase**
<a name="custom-function-huei"></a>

Expand Down
42 changes: 35 additions & 7 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
angular.module('validation.provider', []);
angular.module('validation.directive', ['validation.provider']);
}).call(this);

(function() {
angular
.module('validation.provider')
Expand Down Expand Up @@ -35,6 +35,12 @@
*/
var expression = {};

/**
* default valid method
* @type {{}}
*/
var validMethod = null;

/**
* default error, success message
* @type {{}}
Expand Down Expand Up @@ -79,6 +85,23 @@
return defaultMsg[msg];
};

/**
* allow user to set the global valid method
* @param v
* @returns {*}
*/
this.setValidMethod = function(v) {
validMethod = v;
};

/**
* Get the valid method
* @returns {*}
*/
this.getValidMethod = function() {
return validMethod;
};

/**
* Override the errorHTML function
* @param func
Expand Down Expand Up @@ -247,6 +270,8 @@
this.$get = ['$injector', function($injector) {
setup($injector);
return {
setValidMethod: this.setValidMethod,
getValidMethod: this.getValidMethod,
setErrorHTML: this.setErrorHTML,
getErrorHTML: this.getErrorHTML,
setSuccessHTML: this.setSuccessHTML,
Expand All @@ -266,7 +291,7 @@
}];
}
}).call(this);

(function() {
angular
.module('validation.directive')
Expand All @@ -290,7 +315,7 @@
}
Reset.$inject = ['$injector'];
}).call(this);

(function() {
angular
.module('validation.directive')
Expand Down Expand Up @@ -321,7 +346,7 @@
}
Submit.$inject = ['$injector'];
}).call(this);

(function() {
angular
.module('validation.directive')
Expand Down Expand Up @@ -556,6 +581,9 @@
* Check validator
*/


var validMethod = (angular.isUndefined(attrs.validMethod)) ? $validationProvider.getValidMethod() : attrs.validMethod;

/**
* Click submit form, check the validity when submit
*/
Expand All @@ -565,7 +593,7 @@

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

if (attrs.validMethod === 'submit') {
if (validMethod === 'submit') {
// clear previous scope.$watch
watch();
watch = scope.$watch(function() {
Expand Down Expand Up @@ -606,7 +634,7 @@
/**
* Validate blur method
*/
if (attrs.validMethod === 'blur') {
if (validMethod === 'blur') {
element.bind('blur', function() {
var value = scope.$eval(attrs.ngModel);
scope.$apply(function() {
Expand All @@ -620,7 +648,7 @@
/**
* Validate submit & submit-only method
*/
if (attrs.validMethod === 'submit' || attrs.validMethod === 'submit-only') {
if (validMethod === 'submit' || validMethod === 'submit-only') {
return;
}

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.

25 changes: 25 additions & 0 deletions src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
*/
var expression = {};

/**
* default valid method
* @type {{}}
*/
var validMethod = null;

/**
* default error, success message
* @type {{}}
Expand Down Expand Up @@ -73,6 +79,23 @@
return defaultMsg[msg];
};

/**
* allow user to set the global valid method
* @param v
* @returns {*}
*/
this.setValidMethod = function(v) {
validMethod = v;
};

/**
* Get the valid method
* @returns {*}
*/
this.getValidMethod = function() {
return validMethod;
};

/**
* Override the errorHTML function
* @param func
Expand Down Expand Up @@ -241,6 +264,8 @@
this.$get = ['$injector', function($injector) {
setup($injector);
return {
setValidMethod: this.setValidMethod,
getValidMethod: this.getValidMethod,
setErrorHTML: this.setErrorHTML,
getErrorHTML: this.getErrorHTML,
setSuccessHTML: this.setSuccessHTML,
Expand Down
9 changes: 6 additions & 3 deletions src/validator.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@
* Check validator
*/


var validMethod = (angular.isUndefined(attrs.validMethod)) ? $validationProvider.getValidMethod() : attrs.validMethod;

/**
* Click submit form, check the validity when submit
*/
Expand All @@ -241,7 +244,7 @@

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

if (attrs.validMethod === 'submit') {
if (validMethod === 'submit') {
// clear previous scope.$watch
watch();
watch = scope.$watch(function() {
Expand Down Expand Up @@ -282,7 +285,7 @@
/**
* Validate blur method
*/
if (attrs.validMethod === 'blur') {
if (validMethod === 'blur') {
element.bind('blur', function() {
var value = scope.$eval(attrs.ngModel);
scope.$apply(function() {
Expand All @@ -296,7 +299,7 @@
/**
* Validate submit & submit-only method
*/
if (attrs.validMethod === 'submit' || attrs.validMethod === 'submit-only') {
if (validMethod === 'submit' || validMethod === 'submit-only') {
return;
}

Expand Down
8 changes: 8 additions & 0 deletions test/unit/providerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,12 @@ describe('provider', function() {
expect(successSpy2).not.toHaveBeenCalled();
expect(errorSpy2).toHaveBeenCalled();
}));

it('set/get validMethod', inject(function() {
expect(validationProvider.getValidMethod()).toEqual(null);

validationProvider.setValidMethod('submit');

expect(validationProvider.getValidMethod()).toEqual('submit');
}));
});