diff --git a/README.md b/README.md index 6fb6a7b..5235fa7 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Add Valid Callback Function, `invalid-callback` & `valid-callback` ``` Select the validation method `watch` `blur` `submit`, default as `watch`
-`validationProvider.validate($scope, form).success(callback).error(callback)` use callback to continue your submit +`validationProvider.validate(form).success(callback).error(callback)` use callback to continue your submit ```html @@ -94,7 +94,7 @@ Select the validation method `watch` `blur` `submit`, default as `watch`
// ... validate method, it will check `checkValid(Form)` $scope.form = { submit: function () { - $validationProvider.validate($scope, form) + $validationProvider.validate(form) .success(successCallback) .error(errorCallback); } @@ -187,7 +187,7 @@ Reset the specific Form. `reset()` $scope.form.checkValid = validationProvider.checkValid; // ... reset $scope.form.reset = function (form) { - validationProvider.reset($scope, form); + validationProvider.reset(form); }; ``` diff --git a/demo/demo.js b/demo/demo.js index e50ddb4..5ab55ee 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -13,32 +13,32 @@ angular.module('myApp', ['validation']) requiredCallback: 'required', checkValid: $validationProvider.checkValid, submit: function (form) { - $validationProvider.validate($scope, form); + $validationProvider.validate(form); }, reset: function (form) { - $validationProvider.reset($scope, form); + $validationProvider.reset(form); } }; $scope.form2 = { checkValid: $validationProvider.checkValid, submit: function (form) { - $validationProvider.validate($scope, form); + $validationProvider.validate(form); }, reset: function (form) { - $validationProvider.reset($scope, form); + $validationProvider.reset(form); } }; $scope.form3 = { checkValid: $validationProvider.checkValid, submit: function (form) { - $validationProvider.validate($scope, form) + $validationProvider.validate(form) .success($scope.success) .error($scope.error); }, reset: function (form) { - $validationProvider.reset($scope, form); + $validationProvider.reset(form); } }; diff --git a/dist/angular-validation.js b/dist/angular-validation.js index 24d0f75..d7def5f 100644 --- a/dist/angular-validation.js +++ b/dist/angular-validation.js @@ -7,6 +7,7 @@ var $injector, + $scope, $http, $q, _this = this; @@ -18,6 +19,7 @@ */ var setup = function (injector) { $injector = injector; + $scope = $injector.get('$rootScope'); $http = $injector.get('$http'); $q = $injector.get('$q'); }; @@ -159,15 +161,14 @@ /** * Validate the form when click submit, when `validMethod = submit` - * @param scope * @param form * @returns {promise|*} */ - this.validate = function (scope, form) { + this.validate = function (form) { for (var k in form) { if (form[k].hasOwnProperty('$dirty')) { - scope.$broadcast(k + 'submit'); + $scope.$broadcast(k + 'submit'); } } @@ -199,17 +200,16 @@ /** * reset the specific form - * @param scope * @param form */ - this.reset = function (scope, form) { + this.reset = function (form) { for (var k in form) { if (form[k].hasOwnProperty('$dirty')) { form[k].$setViewValue(null); form[k].$setPristine(); form[k].$setValidity(form[k].$name, false); form[k].$render(); - scope.$broadcast(k + 'reset'); + $scope.$broadcast(k + 'reset'); } } }; diff --git a/dist/angular-validation.min.js b/dist/angular-validation.min.js index 9313fd1..58190e9 100644 --- a/dist/angular-validation.min.js +++ b/dist/angular-validation.min.js @@ -1 +1 @@ -(function(){angular.module("validation",["validation.provider","validation.directive"])}).call(this),function(){angular.module("validation.provider",[]).provider("$validation",function(){var a,b,c,d=this,e=function(d){a=d,b=a.get("$http"),c=a.get("$q")},f={required:/^.+$/,url:/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/,email:/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,number:/^\d+$/},g={required:{error:"This should be Required!!",success:"It's Required"},url:{error:"This should be Url",success:"It's Url"},email:{error:"This should be Email",success:"It's Email"},number:{error:"This should be Number",success:"It's Number"}};this.setExpression=function(a){angular.extend(f,a)},this.getExpression=function(a){return f[a]},this.setDefaultMsg=function(a){angular.extend(g,a)},this.getDefaultMsg=function(a){return g[a]},this.setErrorHTML=function(a){a.constructor===Function&&(d.getErrorHTML=a)},this.getErrorHTML=function(a){return'

'+a+"

"},this.setSuccessHTML=function(a){a.constructor===Function&&(d.getSuccessHTML=a)},this.getSuccessHTML=function(a){return'

'+a+"

"},this.checkValid=function(a){return void 0===a.$valid?!1:a&&a.$valid===!0},this.validate=function(a,b){for(var e in b)b[e].hasOwnProperty("$dirty")&&a.$broadcast(e+"submit");var f=c.defer();return f.promise.success=function(a){return f.promise.then(function(b){a(b)}),f.promise},f.promise.error=function(a){return f.promise.then(null,function(b){a(b)}),f.promise},d.checkValid(b)?f.resolve("success"):f.reject("error"),f.promise},this.reset=function(a,b){for(var c in b)b[c].hasOwnProperty("$dirty")&&(b[c].$setViewValue(null),b[c].$setPristine(),b[c].$setValidity(b[c].$name,!1),b[c].$render(),a.$broadcast(c+"reset"))},this.$get=function(a){return e(a),{setErrorHTML:this.setErrorHTML,getErrorHTML:this.getErrorHTML,setSuccessHTML:this.setSuccessHTML,getSuccessHTML:this.getSuccessHTML,setExpression:this.setExpression,getExpression:this.getExpression,setDefaultMsg:this.setDefaultMsg,getDefaultMsg:this.getDefaultMsg,checkValid:this.checkValid,validate:this.validate,reset:this.reset}}})}.call(this),function(){angular.module("validation.directive",["validation.provider"]).directive("validator",["$injector",function(a){var b=a.get("$validation"),c=a.get("$q"),d=function(a,c,d,e,f){a.next().html(b.getSuccessHTML(c||b.getDefaultMsg(d).success)),f.$setValidity(f.$name,!0),e&&e()},e=function(a,c,d,e,f){a.next().html(b.getErrorHTML(c||b.getDefaultMsg(d).error)),f.$setValidity(f.$name,!1),e&&e()},f=function(a,f,g,h,i,j){var k=i+"SuccessMessage",l=i+"ErrorMessage",m=b.getExpression(i).constructor,n={success:function(){d(f,g[k],i,a.validCallback(),h)},error:function(){e(f,g[l],i,a.invalidCallback(),h)}};return m===Function?c.all([b.getExpression(i)(j)]).then(function(a){return a&&a.length>0&&a[0]?n.success():n.error()},function(){return n.error()}):m===RegExp?b.getExpression(i).test(j)?n.success():n.error():n.error()};return{restrict:"A",require:"ngModel",scope:{model:"=ngModel",validCallback:"&",invalidCallback:"&"},link:function(a,b,c,d){var e=c.validator.split(",");b.after(""),c.$observe("noValidationMessage",function(a){var c=b.next();"true"==a||1==a?c.css("display","none"):("false"==a||0==a)&&c.css("display","block")}),e.forEach(function(e){return d.$setValidity(d.$name,!1),a.$on(d.$name+"submit",function(){var g=b[0].value;f(a,b,c,d,e,g)}),a.$on(d.$name+"reset",function(){b.next().html("")}),"blur"===c.validMethod?(b.bind("blur",function(){var g=b[0].value;a.$apply(function(){f(a,b,c,d,e,g)})}),void 0):("submit"!==c.validMethod&&a.$watch("model",function(g){if(d.$pristine&&d.$viewValue)d.$setViewValue(d.$viewValue);else if(d.$pristine)return b.next().html(""),void 0;f(a,b,c,d,e,g)}),void 0)})}}}])}.call(this); \ No newline at end of file +(function(){angular.module("validation",["validation.provider","validation.directive"])}).call(this),function(){angular.module("validation.provider",[]).provider("$validation",function(){var a,b,c,d,e=this,f=function(e){a=e,b=a.get("$rootScope"),c=a.get("$http"),d=a.get("$q")},g={required:/^.+$/,url:/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/,email:/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/,number:/^\d+$/},h={required:{error:"This should be Required!!",success:"It's Required"},url:{error:"This should be Url",success:"It's Url"},email:{error:"This should be Email",success:"It's Email"},number:{error:"This should be Number",success:"It's Number"}};this.setExpression=function(a){angular.extend(g,a)},this.getExpression=function(a){return g[a]},this.setDefaultMsg=function(a){angular.extend(h,a)},this.getDefaultMsg=function(a){return h[a]},this.setErrorHTML=function(a){a.constructor===Function&&(e.getErrorHTML=a)},this.getErrorHTML=function(a){return'

'+a+"

"},this.setSuccessHTML=function(a){a.constructor===Function&&(e.getSuccessHTML=a)},this.getSuccessHTML=function(a){return'

'+a+"

"},this.checkValid=function(a){return void 0===a.$valid?!1:a&&a.$valid===!0},this.validate=function(a){for(var c in a)a[c].hasOwnProperty("$dirty")&&b.$broadcast(c+"submit");var f=d.defer();return f.promise.success=function(a){return f.promise.then(function(b){a(b)}),f.promise},f.promise.error=function(a){return f.promise.then(null,function(b){a(b)}),f.promise},e.checkValid(a)?f.resolve("success"):f.reject("error"),f.promise},this.reset=function(a){for(var c in a)a[c].hasOwnProperty("$dirty")&&(a[c].$setViewValue(null),a[c].$setPristine(),a[c].$setValidity(a[c].$name,!1),a[c].$render(),b.$broadcast(c+"reset"))},this.$get=function(a){return f(a),{setErrorHTML:this.setErrorHTML,getErrorHTML:this.getErrorHTML,setSuccessHTML:this.setSuccessHTML,getSuccessHTML:this.getSuccessHTML,setExpression:this.setExpression,getExpression:this.getExpression,setDefaultMsg:this.setDefaultMsg,getDefaultMsg:this.getDefaultMsg,checkValid:this.checkValid,validate:this.validate,reset:this.reset}}})}.call(this),function(){angular.module("validation.directive",["validation.provider"]).directive("validator",["$injector",function(a){var b=a.get("$validation"),c=a.get("$q"),d=function(a,c,d,e,f){a.next().html(b.getSuccessHTML(c||b.getDefaultMsg(d).success)),f.$setValidity(f.$name,!0),e&&e()},e=function(a,c,d,e,f){a.next().html(b.getErrorHTML(c||b.getDefaultMsg(d).error)),f.$setValidity(f.$name,!1),e&&e()},f=function(a,f,g,h,i,j){var k=i+"SuccessMessage",l=i+"ErrorMessage",m=b.getExpression(i).constructor,n={success:function(){d(f,g[k],i,a.validCallback(),h)},error:function(){e(f,g[l],i,a.invalidCallback(),h)}};return m===Function?c.all([b.getExpression(i)(j)]).then(function(a){return a&&a.length>0&&a[0]?n.success():n.error()},function(){return n.error()}):m===RegExp?b.getExpression(i).test(j)?n.success():n.error():n.error()};return{restrict:"A",require:"ngModel",scope:{model:"=ngModel",validCallback:"&",invalidCallback:"&"},link:function(a,b,c,d){var e=c.validator.split(",");b.after(""),c.$observe("noValidationMessage",function(a){var c=b.next();"true"==a||1==a?c.css("display","none"):("false"==a||0==a)&&c.css("display","block")}),e.forEach(function(e){return d.$setValidity(d.$name,!1),a.$on(d.$name+"submit",function(){var g=b[0].value;f(a,b,c,d,e,g)}),a.$on(d.$name+"reset",function(){b.next().html("")}),"blur"===c.validMethod?void b.bind("blur",function(){var g=b[0].value;a.$apply(function(){f(a,b,c,d,e,g)})}):void("submit"!==c.validMethod&&a.$watch("model",function(g){if(d.$pristine&&d.$viewValue)d.$setViewValue(d.$viewValue);else if(d.$pristine)return void b.next().html("");f(a,b,c,d,e,g)}))})}}}])}.call(this); \ No newline at end of file diff --git a/src/provider.js b/src/provider.js index 1e83cc2..eaff35e 100644 --- a/src/provider.js +++ b/src/provider.js @@ -4,6 +4,7 @@ var $injector, + $scope, $http, $q, _this = this; @@ -15,6 +16,7 @@ */ var setup = function (injector) { $injector = injector; + $scope = $injector.get('$rootScope'); $http = $injector.get('$http'); $q = $injector.get('$q'); }; @@ -156,15 +158,14 @@ /** * Validate the form when click submit, when `validMethod = submit` - * @param scope * @param form * @returns {promise|*} */ - this.validate = function (scope, form) { + this.validate = function (form) { for (var k in form) { if (form[k].hasOwnProperty('$dirty')) { - scope.$broadcast(k + 'submit'); + $scope.$broadcast(k + 'submit'); } } @@ -196,17 +197,16 @@ /** * reset the specific form - * @param scope * @param form */ - this.reset = function (scope, form) { + this.reset = function (form) { for (var k in form) { if (form[k].hasOwnProperty('$dirty')) { form[k].$setViewValue(null); form[k].$setPristine(); form[k].$setValidity(form[k].$name, false); form[k].$render(); - scope.$broadcast(k + 'reset'); + $scope.$broadcast(k + 'reset'); } } }; diff --git a/test/unit/providerSpec.js b/test/unit/providerSpec.js index abf07f9..607df43 100644 --- a/test/unit/providerSpec.js +++ b/test/unit/providerSpec.js @@ -110,7 +110,7 @@ describe('provider', function () { $scope.$on('requiredreset', function () { resetSpy(); }); - validationProvider.reset($scope, $scope.Form); + validationProvider.reset($scope.Form); expect(element.find('p')[0]).toBeUndefined(); expect(resetSpy).toHaveBeenCalled(); })); @@ -130,7 +130,7 @@ describe('provider', function () { $scope.$apply(function () { $scope.required = 'Required'; }); - validationProvider.validate($scope, $scope.Form) + validationProvider.validate($scope.Form) .success(function () { successSpy(); }) @@ -152,7 +152,7 @@ describe('provider', function () { submitSpy2(); }); - validationProvider.validate($scope, $scope.Form) + validationProvider.validate($scope.Form) .success(function () { successSpy2(); })