/
DatePropertyEditor.cs
64 lines (57 loc) · 2.2 KB
/
DatePropertyEditor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Web.PropertyEditors
{
[PropertyEditor(Constants.PropertyEditors.DateAlias, "Date", "DATE", "datepicker")]
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 PropertyValueEditor CreateValueEditor()
{
return new DatePropertyValueEditor(base.CreateValueEditor());
}
/// <summary>
/// CUstom value editor so we can serialize with the correct date format (excluding time)
/// and includes the date validator
/// </summary>
internal class DatePropertyValueEditor : PropertyValueEditorWrapper
{
public DatePropertyValueEditor(PropertyValueEditor wrapped) : base(wrapped)
{
Validators.Add(new DateTimeValidator());
}
public override object ConvertDbToEditor(Property property, PropertyType propertyType, IDataTypeService dataTypeService)
{
var date = property.Value.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");
}
}
}
}