Got the Date and Date/Time new property editors created, fixed up a few date casting issues with data. These editors 'should' be compatible with the previous legacy date/datetime editors, just need to test it.

This commit is contained in:
Shannon
2013-08-14 20:08:45 +10:00
parent 3e675ff2cc
commit 4a8026c902
13 changed files with 118 additions and 31 deletions

View File

@@ -22,7 +22,7 @@ namespace Umbraco.Web.Models.ContentEditing
public string View { get; set; }
[DataMember(Name = "config")]
public IDictionary<string, string> Config { get; set; }
public IDictionary<string, object> Config { get; set; }
[DataMember(Name = "hideLabel")]
public bool HideLabel { get; set; }

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
@@ -10,13 +11,58 @@ namespace Umbraco.Web.PropertyEditors
[PropertyEditor(Constants.PropertyEditors.Date, "Date", "datepicker", ValueType = "DATE")]
public class DatePropertyEditor : PropertyEditor
{
public DatePropertyEditor()
{
_defaultPreVals = new Dictionary<string, object>
{
{"format", "yyyy-MM-dd"},
{"pickTime", false}
};
}
private IDictionary<string, object> _defaultPreVals;
/// <summary>
/// Overridden because we ONLY support Date (no time) format and we don't have pre-values in the db.
/// </summary>
public override IDictionary<string, object> DefaultPreValues
{
get { return _defaultPreVals; }
set { _defaultPreVals = value; }
}
protected override ValueEditor CreateValueEditor()
{
var editor = base.CreateValueEditor();
var baseEditor = base.CreateValueEditor();
editor.Validators = new List<ValidatorBase> { new DateTimeValidator() };
return new DateValueEditor
{
View = baseEditor.View
};
}
/// <summary>
/// CUstom value editor so we can serialize with the correct date format (excluding time)
/// and includes the date validator
/// </summary>
private class DateValueEditor : ValueEditor
{
public DateValueEditor()
{
Validators = new List<ValidatorBase> { new DateTimeValidator() };
}
public override string SerializeValue(object dbValue)
{
var date = dbValue.TryConvertTo<DateTime?>();
if (date.Success == false || date.Result == null)
{
return string.Empty;
}
//Dates will be formatted as yyyy-MM-dd
return date.Result.Value.ToString("yyyy-MM-dd");
}
return editor;
}
}
}

View File

@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using Umbraco.Core;
using Umbraco.Core.PropertyEditors;
@@ -9,12 +10,18 @@ namespace Umbraco.Web.PropertyEditors
{
public DateTimePropertyEditor()
{
_defaultPreVals = new Dictionary<string, string>() ;
_defaultPreVals = new Dictionary<string, object>
{
{"format", "yyyy-MM-dd HH:mm:ss"}
};
}
private IDictionary<string, string> _defaultPreVals;
private IDictionary<string, object> _defaultPreVals;
public override IDictionary<string, string> DefaultPreValues
/// <summary>
/// Overridden because we ONLY support Date + Time format and we don't have pre-values in the db.
/// </summary>
public override IDictionary<string, object> DefaultPreValues
{
get { return _defaultPreVals; }
set { _defaultPreVals = value; }

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core;
namespace Umbraco.Web.PropertyEditors
{
@@ -13,7 +14,7 @@ namespace Umbraco.Web.PropertyEditors
public override IEnumerable<ValidationResult> Validate(string value, string preValues, PropertyEditor editor)
{
DateTime dt;
if (DateTime.TryParse(value, out dt) == false)
if (value.IsNullOrWhiteSpace() == false && DateTime.TryParse(value, out dt) == false)
{
yield return new ValidationResult(string.Format("The string value {0} cannot be parsed into a DateTime", value),
new[]