diff --git a/ang/crmMailing/RadioDate.js b/ang/crmMailing/RadioDate.js
index 8be382e3d87a..153e9562b930 100644
--- a/ang/crmMailing/RadioDate.js
+++ b/ang/crmMailing/RadioDate.js
@@ -1,7 +1,7 @@
(function(angular, $, _) {
// Represent a datetime field as if it were a radio ('schedule.mode') and a datetime ('schedule.datetime').
// example:
...
- angular.module('crmMailing').directive('crmMailingRadioDate', function() {
+ angular.module('crmMailing').directive('crmMailingRadioDate', function(crmUiAlert) {
return {
require: 'ngModel',
link: function($scope, element, attrs, ngModel) {
@@ -48,6 +48,26 @@
if (context === 'userInput' && $(this).val() === '' && $(this).siblings('.crm-form-date').val().length) {
schedule.mode = 'at';
schedule.datetime = '?';
+ } else {
+ var d = new Date(),
+ month = '' + (d.getMonth() + 1),
+ day = '' + d.getDate(),
+ year = d.getFullYear(),
+ hours = '' + d.getHours(),
+ minutes = '' + d.getMinutes();
+ var submittedDate = $(this).val();
+ if (month.length < 2) month = '0' + month;
+ if (day.length < 2) day = '0' + day;
+ date = [year, month, day].join('-');
+ time = [hours, minutes, "00"].join(':');
+ currentDate = date + ' ' + time;
+ ngModel.$setValidity('dateTimeInThePast', !($(this).val().length && submittedDate < currentDate));
+ if ($(this).val().length && submittedDate < currentDate) {
+ crmUiAlert({
+ text: ts('The scheduled date and time is in the past'),
+ title: ts('Error')
+ });
+ }
}
});
diff --git a/tests/karma/unit/crmMailingRadioDateSpec.js b/tests/karma/unit/crmMailingRadioDateSpec.js
index a90a820f6bb2..4ffee0cc6d7e 100644
--- a/tests/karma/unit/crmMailingRadioDateSpec.js
+++ b/tests/karma/unit/crmMailingRadioDateSpec.js
@@ -80,11 +80,31 @@ describe('crmMailingRadioDate', function() {
model.the_date = '2014-01-02 02:03:00';
$rootScope.$digest();
- expect($rootScope.myForm.$valid).toBe(true);
+ expect($rootScope.myForm.$valid).toBe(false);
expect(element.find('.radio-now').prop('checked')).toBe(false);
expect(element.find('.radio-at').prop('checked')).toBe(true);
expect(element.find('.crm-form-date').datepicker('getDate').toDateString()).toEqual('Thu Jan 02 2014');
expect(element.find('.crm-form-time').timeEntry('getTime').getMinutes()).toBe(3);
+
+ var now = new Date();
+ now.setDate(now.getDate() + 1);
+ var month = '' + (now.getMonth() + 1);
+ var day = '' + now.getDate();
+ var year = now.getFullYear();
+ var hours = '' + now.getHours();
+ var minutes = '' + now.getMinutes();
+ if (month.length < 2) month = '0' + month;
+ if (day.length < 2) day = '0' + day;
+ var datenow = [year, month, day].join('-');
+ var time = [hours, minutes, "00"].join(':');
+ var currentDate = datenow + ' ' + time;
+ var n = now.toDateString();
+ model.the_date = currentDate;
+ $rootScope.$digest();
+ expect($rootScope.myForm.$valid).toBe(true);
+ expect(element.find('.radio-now').prop('checked')).toBe(false);
+ expect(element.find('.radio-at').prop('checked')).toBe(true);
+ expect(element.find('.crm-form-date').datepicker('getDate').toDateString()).toEqual(n);
});
it('should update the model after changing the date and time', function() {
@@ -107,7 +127,7 @@ describe('crmMailingRadioDate', function() {
element.find('.crm-form-time').timeEntry('setTime', '04:05').trigger('change');
$rootScope.$digest();
expect(model.the_date).toBe('2014-01-03 04:05:00');
- expect($rootScope.myForm.$valid).toBe(true);
+ expect($rootScope.myForm.$valid).toBe(false);
expect(element.find('.radio-now').prop('checked')).toBe(false);
expect(element.find('.radio-at').prop('checked')).toBe(true);