#18968: Fixing DateTimeField validation

Work Item: 18968

--HG--
branch : 1.x
This commit is contained in:
Sebastien Ros
2012-09-24 16:12:19 -07:00
parent 7f0dafdd7e
commit 192a8964d5
4 changed files with 17 additions and 5 deletions

View File

@@ -15,6 +15,7 @@ namespace Orchard.Fields.Drivers {
public IOrchardServices Services { get; set; }
private const string TemplateName = "Fields/DateTime.Edit"; // EditorTemplates/Fields/DateTime.Edit.cshtml
private readonly Lazy<CultureInfo> _cultureInfo;
private static readonly DateTime DefaultValue = new DateTime(1980, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public DateTimeFieldDriver(IOrchardServices services) {
Services = services;
@@ -98,7 +99,13 @@ namespace Orchard.Fields.Drivers {
updater.AddModelError(GetPrefix(field, part), T("{0} is required", field.DisplayName));
}
if (DateTime.TryParse(parseDateTime, _cultureInfo.Value, DateTimeStyles.None, out value)) {
if(!settings.Required
&& (settings.Display != DateTimeFieldDisplays.TimeOnly && String.IsNullOrWhiteSpace(viewModel.Date))
|| (settings.Display != DateTimeFieldDisplays.DateOnly && String.IsNullOrWhiteSpace(viewModel.Time))
) {
field.DateTime = DateTime.MinValue;
}
else if (DateTime.TryParse(parseDateTime, _cultureInfo.Value, DateTimeStyles.None, out value)) {
field.DateTime = TimeZoneInfo.ConvertTimeToUtc(value, Services.WorkContext.CurrentTimeZone);
}
else {

View File

@@ -1,4 +1,7 @@
$(document).ready(function () {
var clearHint = function (self) { if (self.val() == self.data("hint")) { self.removeClass("hinted").val("") } };
var resetHint = function (self) { setTimeout(function () { if (!self.val()) { self.addClass("hinted").val(self.data("hint")) } }, 300) };
$(document).ready(function () {
$("label.forpicker").each(function () {
var $this = $(this);
var pickerInput = $("#" + $this.attr("for"));
@@ -6,8 +9,9 @@
if (!pickerInput.val()) {
pickerInput.addClass("hinted")
.val(pickerInput.data("hint"))
.focus(function () { var $this = $(this); if ($this.val() == $this.data("hint")) { $this.removeClass("hinted").val("") } })
.blur(function () { var $this = $(this); setTimeout(function () { if (!$this.val()) { $this.addClass("hinted").val($this.data("hint")) } }, 300) });
.focus(function () { clearHint($(this)); })
.blur(function () { resetHint($(this)); });
$this.closest("form").submit(function () { clearHint(pickerInput); pickerInput = 0; });
}
});
});

View File

@@ -36,6 +36,7 @@
<script type="text/javascript">
//<![CDATA[
$(function () {
$("#@Html.FieldIdFor(m => Model.Date)").datepicker({ showAnim: "" });
@if(Model.ShowTime) {
<text>$("#@Html.FieldIdFor(m => Model.Time)").timepicker({ stepMinute: 5 });</text>

View File

@@ -27,7 +27,7 @@ namespace Orchard.ContentManagement.FieldStorage {
// using a special case for DateTime as it would lose milliseconds otherwise
if (typeof(T) == typeof(DateTime)) {
var result = DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal).ToUniversalTime();
var result = XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Utc);
return (T) (object)result;
}