Skip to content

Instantly share code, notes, and snippets.

@guilbep
Created October 31, 2014 11:48
Show Gist options
  • Save guilbep/55c7b5fa228b139758c1 to your computer and use it in GitHub Desktop.
Save guilbep/55c7b5fa228b139758c1 to your computer and use it in GitHub Desktop.
setValidity
module.directive('setValidity', ['$parse',
function($parse) {
// no need for form. better
return {
require: "ngModel",
scope: {
'setValidity': '=setValidity',
'ngDisabled': '=ngDisabled'
},
controller: function($scope, $element, $attrs, $transclude) {
$scope.isEmpty = function(elements) {
var type = typeof(elements);
var sum = 0;
// what
switch (type) {
case 'object':
angular.forEach(elements, function(value, key) {
sum += value.length;
});
break;
case 'number':
sum = elements;
break;
default:
// It will never work but eyh (in case undefined)
sum = elements || 0;
break;
}
return sum === 0;
};
},
link: function(scope, ele, attrs, c) {
var getValidity = function() {
var disable = scope.ngDisabled;
var empty = scope.isEmpty(scope.setValidity);
return (disable || !empty);
};
c.$setValidity('custom', getValidity());
scope.$watch('setValidity', function(newv) {
c.$setValidity('custom', getValidity());
}, true);
scope.$watch('ngDisabled', function() {
c.$setValidity('custom', getValidity());
});
}
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment