Can the module allow null dates & times?

Apr 19, 2011 at 3:20 PM

Hello,

This module is perfect for some work I'm doing around calendars and events.  The only issue is that at least one of my DateTimes needs to be nullable (think EndDate for an event as it may not need to be provided).  Looking through the code, it seems that if we request both the date and time fields to be shown, then content must be provided or we trip the validation.  It also looks as though the field hints ("Date" and "Time") are being submitted with the form instead of empty values when the fields are left blank.

Do you see this as a bug, or as expected behavior?  Any thoughts on patching the module to only validate the content if it's not empty or looking at the hint js to see why the hints are being submitted?  I don't mind looking into it further, but wanted to make sure that it was necessary for me to do so first.

I can duplicate this module and embed the changes in my new module, but figured others in the community might run into the same issue with reusing this component, so I thought I'd try here first.

Thanks!
-Steve

Dec 14, 2011 at 9:07 AM
Edited Dec 14, 2011 at 9:09 AM

Hi, I use this field for user profiles to add some dates. Sometimes users don't need this date field, but you can not leave it blank. I fixed this problem for me, maybe you need this:

to save not filled fields you need to change this part in 'Drivers/DateTimeFieldDriver.cs'
 

protected override DriverResult Editor(ContentPart part, Fields.DateTimeField field, IUpdateModel updater, dynamic shapeHelper) 
{
	var viewModel = new DateTimeFieldViewModel();

	if ( updater.TryUpdateModel(viewModel, GetPrefix(field, part), null, null) ) {
		DateTime value;

		var settings = field.PartFieldDefinition.Settings.GetModel<DateTimeFieldSettings>();

		if ( viewModel.Time == "Date" || string.IsNullOrEmpty(viewModel.Time) && settings.Display == DateTimeFieldDisplays.TimeOnly ) {
		   field.DateTime = null;
		}
		else if ( viewModel.Date == "Date" || string.IsNullOrEmpty(viewModel.Date) && settings.Display == DateTimeFieldDisplays.DateOnly ) {
		   field.DateTime = null;
		}
		else {
		   if ( settings.Display == DateTimeFieldDisplays.DateOnly ) {
			   viewModel.Time = DateTime.Now.ToShortTimeString();
		   }

		   if ( settings.Display == DateTimeFieldDisplays.TimeOnly ) {
			   viewModel.Date = DateTime.Now.ToShortDateString();
		   }

		   if (DateTime.TryParse(viewModel.Date + " " + viewModel.Time, out value)) {
			   field.DateTime = value.ToUniversalTime();
		   }
		   else {
			   updater.AddModelError(GetPrefix(field, part), T("{0} is an invalid date and time", field.Name));
			   field.DateTime = null;
		   }
		}
	}

	return Editor(part, field, shapeHelper); 
}

 

 

and for 'Views/Fields/Contrib.DateTime.cshtml' to hide empty fields

 

@using Orchard.Utility.Extensions;
@if(string.IsNullOrEmpty(Model.Model.Date) || Model.Model.Date == "Date" || string.IsNullOrEmpty(Model.Model.Time) || Model.Model.Time == "Time") {
}
else {
<p class="text-field"><span class="name">@Model.Model.Name:</span> 
    @if(Model.Model.ShowDate) { <text>@Model.Model.Date</text> } @if(Model.Model.ShowTime) { <text>@Model.Model.Time</text> }
</p>
}



greetings Sebastian