diff --git a/src/Umbraco.Web/PropertyEditors/ColorListPreValueEditor.cs b/src/Umbraco.Web/PropertyEditors/ColorListPreValueEditor.cs index 7a5475406c..2963ac6a35 100644 --- a/src/Umbraco.Web/PropertyEditors/ColorListPreValueEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ColorListPreValueEditor.cs @@ -11,14 +11,19 @@ namespace Umbraco.Web.PropertyEditors { internal class ColorListPreValueEditor : ValueListPreValueEditor { + public ColorListPreValueEditor() { + var field = Fields.First(); + //use a custom editor too - Fields.First().View = "views/propertyeditors/colorpicker/colorpicker.prevalues.html"; + field.View = "views/propertyeditors/colorpicker/colorpicker.prevalues.html"; //change the description - Fields.First().Description = "Add and remove colors in HEX format without a prefixed '#'"; + field.Description = "Add and remove colors in HEX format without a prefixed '#'"; + //change the label + field.Name = "Add color"; //need to have some custom validation happening here - Fields.First().Validators.Add(new ColorListValidator()); + field.Validators.Add(new ColorListValidator()); } internal class ColorListValidator : IPropertyValidator diff --git a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs index 5d9e7d0a85..36386be99c 100644 --- a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Drawing; using System.Linq; +using System.Text.RegularExpressions; using System.Xml; +using Newtonsoft.Json.Linq; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; @@ -126,6 +129,16 @@ namespace Umbraco.Web.PropertyEditors /// internal class FileUploadPreValueEditor : ValueListPreValueEditor { + public FileUploadPreValueEditor() + : base() + { + var field = Fields.First(); + field.Description = "Enter a max width/height for each thumbnail"; + field.Name = "Add thumbnail size"; + //need to have some custom validation happening here + field.Validators.Add(new ThumbnailListValidator()); + } + /// /// Format the persisted value to work with our multi-val editor. /// @@ -164,12 +177,43 @@ namespace Umbraco.Web.PropertyEditors //this should just be a dictionary of values, we want to re-format this so that it is just one value in the dictionary that is // semi-colon delimited - var values = result.Select(item => item.Value).ToList(); + var values = result.Select(item => item.Value.Value).ToList(); result.Clear(); result.Add("thumbs", new PreValue(string.Join(";", values))); return result; } + + internal class ThumbnailListValidator : IPropertyValidator + { + public IEnumerable Validate(object value, PreValueCollection preValues, PropertyEditor editor) + { + var json = value as JArray; + if (json == null) yield break; + + //validate each item which is a json object + for (var index = 0; index < json.Count; index++) + { + var i = json[index]; + var jItem = i as JObject; + if (jItem == null || jItem["value"] == null) continue; + + //NOTE: we will be removing empty values when persisting so no need to validate + var asString = jItem["value"].ToString(); + if (asString.IsNullOrWhiteSpace()) continue; + + int parsed; + if (int.TryParse(asString, out parsed) == false) + { + yield return new ValidationResult("The value " + asString + " is not a valid number", new[] + { + //we'll make the server field the index number of the value so it can be wired up to the view + "item_" + index.ToInvariantString() + }); + } + } + } + } } }