CRM-15856 - crmMailingRecipients - Respect ngRequired
authorTim Otten <totten@civicrm.org>
Thu, 12 Feb 2015 00:31:53 +0000 (16:31 -0800)
committerTim Otten <totten@civicrm.org>
Thu, 12 Feb 2015 19:59:17 +0000 (11:59 -0800)
js/angular-crmMailing/directives.js
partials/crmMailing/recipients.html

index d86fed8cfaae560e4d1e0cc89fa3eaa267aed1ea..1ba5449fbe065c6698fb3fea3704e78f0a663779 100644 (file)
       scope: {
         crmAvailGroups: '@', // available groups
         crmAvailMailings: '@', // available mailings
+        ngRequired: '@'
       },
       templateUrl: '~/crmMailing/directive/recipients.html',
       link: function (scope, element, attrs, ngModel) {
           scope.recips = ngModel.$viewValue;
           if (ngModel.$viewValue) {
             $(element).select2('val', convertMailingToValues(ngModel.$viewValue));
+            validate();
           }
         };
 
           return "<img src='../../sites/all/modules/civicrm/i/" + icon + "' height=12 width=12 /> <span class='" + spanClass + "'>" + item.text + "</span>";
         }
 
+        function validate() {
+          if (scope.$parent.$eval(attrs.ngRequired)) {
+            var empty = (_.isEmpty(ngModel.$viewValue.groups.include) && _.isEmpty(ngModel.$viewValue.mailings.include));
+            ngModel.$setValidity('empty', !empty);
+          } else {
+            ngModel.$setValidity('empty', true);
+          }
+        }
+
         $(element).select2({
           dropdownAutoWidth: true,
           placeholder: "Groups or Past Recipients",
           formatSelection: formatItem,
           escapeMarkup: function (m) {
             return m;
-          },
+          }
         });
 
         $(element).on('select2-selecting', function (e) {
           }
           scope.$apply();
           $(element).select2('close');
+          validate();
           e.preventDefault();
         });
 
           scope.$parent.$apply(function () {
             arrayRemove(ngModel.$viewValue[typeKey][option.mode], option.entity_id);
           });
+          validate();
           e.preventDefault();
         });
 
index 3306ef4aea2bac5c5926b3b69b102a0e81f80ed0..90d1918540e797355418c851cfe4064bd47664f7 100644 (file)
@@ -22,7 +22,7 @@
     crm-avail-mailings="crmMailingConst.civiMails | filter:{is_completed:1}"
     crm-ui-id="{{crmMailingBlockRecipients.id}}"
     name="{{crmMailingBlockRecipients.name}}"
-    required
+    ng-required="true"
     multiple>
   </select>
 </div>