Color picker is working, added custom validation to the pre-values but need to get them working a bit better and showing the colors in the pre-val editor as well. Then also need to update the other list pre-value editors to be able to update values so we don't have to remove and re-add them.

This commit is contained in:
Shannon
2013-08-27 15:02:24 +10:00
parent 3e95b0468f
commit e448436614
13 changed files with 136 additions and 12 deletions

View File

@@ -88,7 +88,7 @@ namespace Umbraco.Web.Editors
foreach (var v in propertyEditor.PreValueEditor.Fields.SelectMany(x => x.Validators))
{
foreach (var result in v.Validate(postedValue != null ? postedValue.ToString() : null, preVal.Key, propertyEditor))
foreach (var result in v.Validate(postedValue, preVal.Key, propertyEditor))
{
//if there are no member names supplied then we assume that the validation message is for the overall property
// not a sub field on the property editor

View File

@@ -0,0 +1,73 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using Umbraco.Core;
using Umbraco.Core.PropertyEditors;
namespace Umbraco.Web.PropertyEditors
{
[PropertyEditor(Constants.PropertyEditors.ColorPicker, "Color Picker", "colorpicker")]
public class ColorPickerPropertyEditor : PropertyEditor
{
/// <summary>
/// Return a custom pre-value editor
/// </summary>
/// <returns></returns>
/// <remarks>
/// We are just going to re-use the ValueListPreValueEditor
/// </remarks>
protected override PreValueEditor CreatePreValueEditor()
{
return new ColorListPreValueEditor();
}
}
internal class ColorListPreValueEditor : ValueListPreValueEditor
{
public ColorListPreValueEditor()
{
var fields = CreatePreValueFields();
//change the description
fields.First().Description = "Add and remove colors in HEX format without a prefixed '#'";
//need to have some custom validation happening here
fields.First().Validators = new List<ValidatorBase>
{
new ColorListValidator()
};
Fields = fields;
}
internal class ColorListValidator : ValidatorBase
{
public override IEnumerable<ValidationResult> Validate(object value, string preValues, PropertyEditor editor)
{
var json = value as JArray;
if (json != null)
{
//validate each item
foreach (var i in json)
{
//NOTE: we will be removing empty values when persisting so no need to validate
var asString = i.ToString();
if (asString.IsNullOrWhiteSpace() == false)
{
if (Regex.IsMatch(asString, "^([0-9a-f]{3}|[0-9a-f]{6})$", RegexOptions.IgnoreCase) == false)
{
yield return new ValidationResult("The value " + asString + " is not a valid hex color", new[]
{
//we'll make the server field name the value of the hex color so we can wire it back up to the
//individual row in the UI.
asString
});
}
}
}
}
}
}
}
}

View File

@@ -4,11 +4,13 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Models;
using Umbraco.Core.PropertyEditors;
using Umbraco.Core.Services;
namespace Umbraco.Web.PropertyEditors
{
[PropertyEditor(Constants.PropertyEditors.Textbox, "Textstring", "textstring")]
[PropertyEditor(Constants.PropertyEditors.Textbox, "Textbox", "textbox")]
public class TextStringPropertyEditor : PropertyEditor
{
}

View File

@@ -91,6 +91,12 @@ namespace Umbraco.Web.PropertyEditors
var index = 0;
foreach (var item in val)
{
var asString = item.ToString();
//don't allow empties
if (asString.IsNullOrWhiteSpace())
{
continue;
}
result.Add(index.ToInvariantString(), item.ToString());
index++;
}

View File

@@ -316,6 +316,7 @@
<Compile Include="Models\Mapping\PreValueDisplayResolver.cs" />
<Compile Include="Models\PagedResult.cs" />
<Compile Include="PropertyEditors\CheckBoxListPropertyEditor.cs" />
<Compile Include="PropertyEditors\ColorPickerPropertyEditor.cs" />
<Compile Include="PropertyEditors\DatePropertyEditor.cs" />
<Compile Include="PropertyEditors\DateTimePropertyEditor.cs" />
<Compile Include="PropertyEditors\DateTimeValidator.cs" />