xebia/angular-form-messages-example

View on GitHub
app/scripts/checkboxesRequiredDirective.js

Summary

Maintainability
A
0 mins
Test Coverage
angular.module('angularFormMessagesExample')
  .directive('checkboxesRequired', function () {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function ($scope, element, attrs, ngModelCtrl) {

        // Map UI onto $viewValue by explicitely change it, because render does no deep watch
        $scope.$watchCollection(attrs.ngModel, function (newVal, oldVal) {
          if (angular.equals(newVal, oldVal)) { return; }
          ngModelCtrl.$setViewValue(angular.extend({}, newVal));
        });

        ngModelCtrl.$validators.required = function (modelValue, viewValue) {
          var
            value = modelValue || viewValue,
            isValid = false;

          if (!ngModelCtrl.$isEmpty(value)) {
            angular.forEach(value, function (item) {
              isValid = isValid || !!item;
            });
          }
          return isValid;
        };
      }
    };
  });