diff --git a/dist/angular-validation.js b/dist/angular-validation.js index 72ac08c..409aef2 100644 --- a/dist/angular-validation.js +++ b/dist/angular-validation.js @@ -183,9 +183,13 @@ return deferred.promise; } - for (var k in form) { - if (form[k].hasOwnProperty('$dirty')) { - $scope.$broadcast(k + 'submit-' + form[k].validationId, idx++); + if (form.validationId) { + $scope.$broadcast(form.$name + 'submit-' + form.validationId, idx++); + } else { + for (var k in form) { + if (form[k] && form[k].hasOwnProperty('$dirty')) { + $scope.$broadcast(k + 'submit-' + form[k].validationId, idx++); + } } } @@ -221,9 +225,19 @@ * @param form */ this.reset = function (form) { - for (var k in form) { - if (form[k].hasOwnProperty('$dirty')) { - $scope.$broadcast(k + 'reset-' + form[k].validationId); + + if (form === undefined) { + console.error('This is not a regular Form name scope'); + return; + } + + if (form.validationId) { + $scope.$broadcast(form.$name + 'reset-' + form.validationId); + } else { + for (var k in form) { + if (form[k].hasOwnProperty('$dirty')) { + $scope.$broadcast(k + 'reset-' + form[k].validationId); + } } } }; @@ -552,13 +566,13 @@ priority: 1, // execute before ng-click (0) terminal: true, link: function postLink(scope, element, attrs) { - var form = attrs.validationSubmit; + var form = $parse(attrs.validationSubmit)(scope); $timeout(function () { element.on('click', function (e) { e.preventDefault(); - $validationProvider.validate(scope[form]) + $validationProvider.validate(form) .success(function () { $parse(attrs.ngClick)(scope); }); @@ -572,16 +586,17 @@ .directive('validationReset', ['$injector', function ($injector) { var $validationProvider = $injector.get('$validation'), - $timeout = $injector.get('$timeout'); + $timeout = $injector.get('$timeout'), + $parse = $injector.get('$parse'); return { link: function postLink(scope, element, attrs) { - var form = attrs.validationReset; + var form = $parse(attrs.validationReset)(scope); $timeout(function () { element.on('click', function (e) { e.preventDefault(); - $validationProvider.reset(scope[form]); + $validationProvider.reset(form); }); }); diff --git a/dist/angular-validation.min.js b/dist/angular-validation.min.js index d29cbc5..c99d73e 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,e,f=this,g=function(f){a=f,b=a.get("$rootScope"),c=a.get("$http"),d=a.get("$q"),e=a.get("$timeout")},h={},i={};this.setExpression=function(a){return angular.extend(h,a),f},this.getExpression=function(a){return h[a]},this.setDefaultMsg=function(a){return angular.extend(i,a),f},this.getDefaultMsg=function(a){return i[a]},this.setErrorHTML=function(a){return a.constructor===Function?(f.getErrorHTML=a,f):void 0},this.getErrorHTML=function(a){return'
'+a+"
"},this.setSuccessHTML=function(a){return a.constructor===Function?(f.getSuccessHTML=a,f):void 0},this.getSuccessHTML=function(a){return''+a+"
"},this.showSuccessMessage=!0,this.showErrorMessage=!0,this.checkValid=function(a){return void 0===a.$valid?!1:a&&a.$valid===!0},this.validate=function(a){var c=d.defer(),g=0;if(void 0===a)return console.error("This is not a regular Form name scope"),c.reject("This is not a regular Form name scope"),c.promise;for(var h in a)a[h].hasOwnProperty("$dirty")&&b.$broadcast(h+"submit-"+a[h].validationId,g++);return c.promise.success=function(a){return c.promise.then(function(b){a(b)}),c.promise},c.promise.error=function(a){return c.promise.then(null,function(b){a(b)}),c.promise},e(function(){f.checkValid(a)?c.resolve("success"):c.reject("error")}),c.promise},this.reset=function(a){for(var c in a)a[c].hasOwnProperty("$dirty")&&b.$broadcast(c+"reset-"+a[c].validationId)},this.$get=["$injector",function(a){return g(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,showSuccessMessage:this.showSuccessMessage,showErrorMessage:this.showErrorMessage,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=a.get("$timeout"),e=function(a,c,d,e,f){return a.next().html(b.showSuccessMessage?b.getSuccessHTML(c||b.getDefaultMsg(d).success):""),f.$setValidity(f.$name,!0),e&&e(),!0},f=function(a,c,d,e,f){return a.next().html(b.showErrorMessage?b.getErrorHTML(c||b.getDefaultMsg(d).error):""),f.$setValidity(f.$name,!1),e&&e(),!1},g=!1,h=function(a,d,g,h,i,j){var k=i+"SuccessMessage",l=i+"ErrorMessage",m=b.getExpression(i).constructor,n={success:function(){return e(d,g[k],i,a.validCallback,h)},error:function(){return f(d,g[l],i,a.invalidCallback,h)}};return m===Function?c.all([b.getExpression(i)(j,a,d,g)]).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()},i=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)},j=function(){return i()+i()+i()+i()};return{restrict:"A",require:"ngModel",scope:{model:"=ngModel",validCallback:"&",invalidCallback:"&"},link:function(a,b,c,e){var f=function(){},i=c.validator.split(","),k=e.validationId=j();b.after(""),e.$setValidity(e.$name,!1),a.$on(e.$name+"reset-"+k,function(){f(),g=!1,e.$setViewValue(""),e.$setPristine(),e.$setValidity(e.$name,!1),e.$render(),b.next().html("")}),i.forEach(function(d){return a.$on(e.$name+"submit-"+k,function(i,j){var k=b[0].value,l=!1;0==j&&(g=!1),l=h(a,b,c,e,d,k),"submit"===c.validMethod&&(f(),f=a.$watch("model",function(f,g){f!==g&&((void 0===f||null===f)&&(f=""),l=h(a,b,c,e,d,f))})),g||l||(g=!0,b[0].focus())}),"blur"===c.validMethod?void b.bind("blur",function(){var f=b[0].value;a.$apply(function(){h(a,b,c,e,d,f)})}):void("submit"!==c.validMethod&&"submit-only"!==c.validMethod&&a.$watch("model",function(f){if(e.$pristine&&e.$viewValue)e.$setViewValue(e.$viewValue);else if(e.$pristine)return void b.next().html("");h(a,b,c,e,d,f)}))}),d(function(){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")})})}}}]).directive("validationSubmit",["$injector",function(a){var b=a.get("$validation"),c=a.get("$timeout"),d=a.get("$parse");return{priority:1,terminal:!0,link:function(a,e,f){var g=f.validationSubmit;c(function(){e.on("click",function(c){c.preventDefault(),b.validate(a[g]).success(function(){d(f.ngClick)(a)})})})}}}]).directive("validationReset",["$injector",function(a){var b=a.get("$validation"),c=a.get("$timeout");return{link:function(a,d,e){var f=e.validationReset;c(function(){d.on("click",function(c){c.preventDefault(),b.reset(a[f])})})}}}])}.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,f=this,g=function(f){a=f,b=a.get("$rootScope"),c=a.get("$http"),d=a.get("$q"),e=a.get("$timeout")},h={},i={};this.setExpression=function(a){return angular.extend(h,a),f},this.getExpression=function(a){return h[a]},this.setDefaultMsg=function(a){return angular.extend(i,a),f},this.getDefaultMsg=function(a){return i[a]},this.setErrorHTML=function(a){return a.constructor===Function?(f.getErrorHTML=a,f):void 0},this.getErrorHTML=function(a){return''+a+"
"},this.setSuccessHTML=function(a){return a.constructor===Function?(f.getSuccessHTML=a,f):void 0},this.getSuccessHTML=function(a){return''+a+"
"},this.showSuccessMessage=!0,this.showErrorMessage=!0,this.checkValid=function(a){return void 0===a.$valid?!1:a&&a.$valid===!0},this.validate=function(a){var c=d.defer(),g=0;if(void 0===a)return console.error("This is not a regular Form name scope"),c.reject("This is not a regular Form name scope"),c.promise;if(a.validationId)b.$broadcast(a.$name+"submit-"+a.validationId,g++);else for(var h in a)a[h]&&a[h].hasOwnProperty("$dirty")&&b.$broadcast(h+"submit-"+a[h].validationId,g++);return c.promise.success=function(a){return c.promise.then(function(b){a(b)}),c.promise},c.promise.error=function(a){return c.promise.then(null,function(b){a(b)}),c.promise},e(function(){f.checkValid(a)?c.resolve("success"):c.reject("error")}),c.promise},this.reset=function(a){if(void 0===a)return void console.error("This is not a regular Form name scope");if(a.validationId)b.$broadcast(a.$name+"reset-"+a.validationId);else for(var c in a)a[c].hasOwnProperty("$dirty")&&b.$broadcast(c+"reset-"+a[c].validationId)},this.$get=["$injector",function(a){return g(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,showSuccessMessage:this.showSuccessMessage,showErrorMessage:this.showErrorMessage,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=a.get("$timeout"),e=function(a,c,d,e,f){return a.next().html(b.showSuccessMessage?b.getSuccessHTML(c||b.getDefaultMsg(d).success):""),f.$setValidity(f.$name,!0),e&&e(),!0},f=function(a,c,d,e,f){return a.next().html(b.showErrorMessage?b.getErrorHTML(c||b.getDefaultMsg(d).error):""),f.$setValidity(f.$name,!1),e&&e(),!1},g=!1,h=function(a,d,g,h,i,j){var k=i+"SuccessMessage",l=i+"ErrorMessage",m=b.getExpression(i).constructor,n={success:function(){return e(d,g[k],i,a.validCallback,h)},error:function(){return f(d,g[l],i,a.invalidCallback,h)}};return m===Function?c.all([b.getExpression(i)(j,a,d,g)]).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()},i=function(){return(65536*(1+Math.random())|0).toString(16).substring(1)},j=function(){return i()+i()+i()+i()};return{restrict:"A",require:"ngModel",scope:{model:"=ngModel",validCallback:"&",invalidCallback:"&"},link:function(a,b,c,e){var f=function(){},i=c.validator.split(","),k=e.validationId=j();b.after(""),e.$setValidity(e.$name,!1),a.$on(e.$name+"reset-"+k,function(){f(),g=!1,e.$setViewValue(""),e.$setPristine(),e.$setValidity(e.$name,!1),e.$render(),b.next().html("")}),i.forEach(function(d){return a.$on(e.$name+"submit-"+k,function(i,j){var k=b[0].value,l=!1;0==j&&(g=!1),l=h(a,b,c,e,d,k),"submit"===c.validMethod&&(f(),f=a.$watch("model",function(f,g){f!==g&&((void 0===f||null===f)&&(f=""),l=h(a,b,c,e,d,f))})),g||l||(g=!0,b[0].focus())}),"blur"===c.validMethod?void b.bind("blur",function(){var f=b[0].value;a.$apply(function(){h(a,b,c,e,d,f)})}):void("submit"!==c.validMethod&&"submit-only"!==c.validMethod&&a.$watch("model",function(f){if(e.$pristine&&e.$viewValue)e.$setViewValue(e.$viewValue);else if(e.$pristine)return void b.next().html("");h(a,b,c,e,d,f)}))}),d(function(){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")})})}}}]).directive("validationSubmit",["$injector",function(a){var b=a.get("$validation"),c=a.get("$timeout"),d=a.get("$parse");return{priority:1,terminal:!0,link:function(a,e,f){var g=d(f.validationSubmit)(a);c(function(){e.on("click",function(c){c.preventDefault(),b.validate(g).success(function(){d(f.ngClick)(a)})})})}}}]).directive("validationReset",["$injector",function(a){var b=a.get("$validation"),c=a.get("$timeout"),d=a.get("$parse");return{link:function(a,e,f){var g=d(f.validationReset)(a);c(function(){e.on("click",function(a){a.preventDefault(),b.reset(g)})})}}}])}.call(this); \ No newline at end of file diff --git a/src/directive.js b/src/directive.js index 26fca81..7b6c292 100644 --- a/src/directive.js +++ b/src/directive.js @@ -295,13 +295,13 @@ priority: 1, // execute before ng-click (0) terminal: true, link: function postLink(scope, element, attrs) { - var form = attrs.validationSubmit; + var form = $parse(attrs.validationSubmit)(scope); $timeout(function () { element.on('click', function (e) { e.preventDefault(); - $validationProvider.validate(scope[form]) + $validationProvider.validate(form) .success(function () { $parse(attrs.ngClick)(scope); }); @@ -315,16 +315,17 @@ .directive('validationReset', ['$injector', function ($injector) { var $validationProvider = $injector.get('$validation'), - $timeout = $injector.get('$timeout'); + $timeout = $injector.get('$timeout'), + $parse = $injector.get('$parse'); return { link: function postLink(scope, element, attrs) { - var form = attrs.validationReset; + var form = $parse(attrs.validationReset)(scope); $timeout(function () { element.on('click', function (e) { e.preventDefault(); - $validationProvider.reset(scope[form]); + $validationProvider.reset(form); }); }); diff --git a/src/provider.js b/src/provider.js index 4f7763b..c21a894 100644 --- a/src/provider.js +++ b/src/provider.js @@ -180,9 +180,13 @@ return deferred.promise; } - for (var k in form) { - if (form[k].hasOwnProperty('$dirty')) { - $scope.$broadcast(k + 'submit-' + form[k].validationId, idx++); + if (form.validationId) { + $scope.$broadcast(form.$name + 'submit-' + form.validationId, idx++); + } else { + for (var k in form) { + if (form[k] && form[k].hasOwnProperty('$dirty')) { + $scope.$broadcast(k + 'submit-' + form[k].validationId, idx++); + } } } @@ -218,9 +222,19 @@ * @param form */ this.reset = function (form) { - for (var k in form) { - if (form[k].hasOwnProperty('$dirty')) { - $scope.$broadcast(k + 'reset-' + form[k].validationId); + + if (form === undefined) { + console.error('This is not a regular Form name scope'); + return; + } + + if (form.validationId) { + $scope.$broadcast(form.$name + 'reset-' + form.validationId); + } else { + for (var k in form) { + if (form[k].hasOwnProperty('$dirty')) { + $scope.$broadcast(k + 'reset-' + form[k].validationId); + } } } };