mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
#18968: Fixing DateTimeField validation
Work Item: 18968 --HG-- branch : 1.x
This commit is contained in:
@@ -15,6 +15,7 @@ namespace Orchard.Fields.Drivers {
|
|||||||
public IOrchardServices Services { get; set; }
|
public IOrchardServices Services { get; set; }
|
||||||
private const string TemplateName = "Fields/DateTime.Edit"; // EditorTemplates/Fields/DateTime.Edit.cshtml
|
private const string TemplateName = "Fields/DateTime.Edit"; // EditorTemplates/Fields/DateTime.Edit.cshtml
|
||||||
private readonly Lazy<CultureInfo> _cultureInfo;
|
private readonly Lazy<CultureInfo> _cultureInfo;
|
||||||
|
private static readonly DateTime DefaultValue = new DateTime(1980, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||||
|
|
||||||
public DateTimeFieldDriver(IOrchardServices services) {
|
public DateTimeFieldDriver(IOrchardServices services) {
|
||||||
Services = services;
|
Services = services;
|
||||||
@@ -98,7 +99,13 @@ namespace Orchard.Fields.Drivers {
|
|||||||
updater.AddModelError(GetPrefix(field, part), T("{0} is required", field.DisplayName));
|
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);
|
field.DateTime = TimeZoneInfo.ConvertTimeToUtc(value, Services.WorkContext.CurrentTimeZone);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -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 () {
|
$("label.forpicker").each(function () {
|
||||||
var $this = $(this);
|
var $this = $(this);
|
||||||
var pickerInput = $("#" + $this.attr("for"));
|
var pickerInput = $("#" + $this.attr("for"));
|
||||||
@@ -6,8 +9,9 @@
|
|||||||
if (!pickerInput.val()) {
|
if (!pickerInput.val()) {
|
||||||
pickerInput.addClass("hinted")
|
pickerInput.addClass("hinted")
|
||||||
.val(pickerInput.data("hint"))
|
.val(pickerInput.data("hint"))
|
||||||
.focus(function () { var $this = $(this); if ($this.val() == $this.data("hint")) { $this.removeClass("hinted").val("") } })
|
.focus(function () { clearHint($(this)); })
|
||||||
.blur(function () { var $this = $(this); setTimeout(function () { if (!$this.val()) { $this.addClass("hinted").val($this.data("hint")) } }, 300) });
|
.blur(function () { resetHint($(this)); });
|
||||||
|
$this.closest("form").submit(function () { clearHint(pickerInput); pickerInput = 0; });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+1
@@ -36,6 +36,7 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
//<![CDATA[
|
//<![CDATA[
|
||||||
$(function () {
|
$(function () {
|
||||||
|
|
||||||
$("#@Html.FieldIdFor(m => Model.Date)").datepicker({ showAnim: "" });
|
$("#@Html.FieldIdFor(m => Model.Date)").datepicker({ showAnim: "" });
|
||||||
@if(Model.ShowTime) {
|
@if(Model.ShowTime) {
|
||||||
<text>$("#@Html.FieldIdFor(m => Model.Time)").timepicker({ stepMinute: 5 });</text>
|
<text>$("#@Html.FieldIdFor(m => Model.Time)").timepicker({ stepMinute: 5 });</text>
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Orchard.ContentManagement.FieldStorage {
|
|||||||
|
|
||||||
// using a special case for DateTime as it would lose milliseconds otherwise
|
// using a special case for DateTime as it would lose milliseconds otherwise
|
||||||
if (typeof(T) == typeof(DateTime)) {
|
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;
|
return (T) (object)result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user