Block Past Event Creation

Sometimes we don't want users to be able to create events in the past or to alter past events. This might be role based in some cases or applied in general to the calendar.

To disable past event creation and manipulation, a couple of things need to be handled:

  • Invalidate dates that are before today - this takes care of validating event drop and event creation on past dates
  • Mark past events as fixed - to do this, set the editable property of the event object to false for the events that should remain fixed
  • Handle past occurrences of recurring events - recurring events are loaded as a single event and the occurrences are generated by the event calendar. Moving the past occurrences need to be handled in the onEventUpdate and onEventCreate lifecycle events
  • Give feedback to the user - optionally, a toast can be displayed to give feedback to the user why an event cannot be dropped, moved and created in the past. For this we can use the onEventCreateFailed and onEventUpdateFailed lifecycle events
Download and try example

Demo

Loading the live demo

Preview in full screen

Keep past dates read-only in a event calendar — block creation and editing on past days and show toast feedback on failed attempts.

Code

mobiscroll.setOptions({
  locale:  ,
  theme: ' ',
  themeVariant: ' '
});

var today = new Date();

var inst = mobiscroll.eventcalendar('#demo-disallow-past-event-creation', {
  view: {
    calendar: {
      labels: true,
    },
  },
  invalid: [
    {
      recurring: {
        repeat: 'daily',
        until: today,
      },
    },
  ],
  clickToCreate: true,
  dragToCreate: true,
  dragToMove: true,
  dragToResize: true,
  onEventCreateFailed: function (args) {
    if (!args.originEvent) {
      mobiscroll.toast({
        message: "Can't create event in the past",
      });
    }
  },
  onEventUpdateFailed: function (args) {
    if (!args.oldEventOccurrence) {
      mobiscroll.toast({
        message: "Can't move event in the past",
      });
    }
  },
  onEventCreate: function (args) {
    var oldEvent = args.originEvent;
    var start = oldEvent && oldEvent.start ? oldEvent.start : null;

    // Handle recurring events
    if (start && start < today) {
      inst.updateEvent(oldEvent);
      mobiscroll.toast({
        message: "Can't move past event",
      });
      return false;
    }
  },
  onEventUpdate: function (args) {
    var oldEvent = args.oldEvent;
    var start = oldEvent && oldEvent.start ? oldEvent.start : null;
    var oldEventOccurrence = args.oldEventOccurrence;
    var occurrenceStart = oldEventOccurrence && oldEventOccurrence.start ? oldEventOccurrence.start : null;

    // Handle recurring events
    if ((start && start < today) || (occurrenceStart && occurrenceStart < today)) {
      inst.updateEvent(oldEvent);
      return false;
    }
  },
});

mobiscroll.getJson(
  'https://trial.mobiscroll.com/events/?vers=5',
  function (events) {
    for (var i = 0; i < events.length; ++i) {
      var event = events[i];
      // Convert dates to date objects
      event.start = event.start ? new Date(event.start) : event.start;
      event.end = event.end ? new Date(event.end) : event.end;
      // Mark past events as fixed by setting the event.editable property to false
      event.editable = event.start && today < event.start;
    }
    inst.setEvents(events);
  },
  'jsonp',
);
  

Duplicate Session

You have signed in on a different machine. To keep everything in sync, you have been signed out of this session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.

Duplicate Session

We noticed you are logged in on a different machine and have closed your old session.

If you need to invite team members, manage seats and accounts you can do it from the licenses page.