From 66b709653ae0c81762a6a0fdd8331f4556e40eb5 Mon Sep 17 00:00:00 2001 From: Andy Butland Date: Sat, 12 Oct 2019 19:38:44 +0200 Subject: [PATCH] Added custom FormatValidator for the MultipleTextStringPropertyEditor to validate each text string individually against the configured regular expression. --- .../MultipleTextStringPropertyEditor.cs | 39 +++++++++++++++++-- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Web/PropertyEditors/MultipleTextStringPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/MultipleTextStringPropertyEditor.cs index 387fc670b8..fa82bc555c 100644 --- a/src/Umbraco.Web/PropertyEditors/MultipleTextStringPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/MultipleTextStringPropertyEditor.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using Newtonsoft.Json.Linq; using Umbraco.Core; @@ -7,6 +9,7 @@ using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.Editors; using Umbraco.Core.PropertyEditors; +using Umbraco.Core.PropertyEditors.Validators; using Umbraco.Core.Services; namespace Umbraco.Web.PropertyEditors @@ -86,7 +89,7 @@ namespace Umbraco.Web.PropertyEditors /// /// /// - /// + /// /// /// /// @@ -97,10 +100,40 @@ namespace Umbraco.Web.PropertyEditors var val = property.GetValue(culture, segment); return val?.ToString().Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries) .Select(x => JObject.FromObject(new {value = x})) ?? new JObject[] { }; - - } + /// + /// A custom FormatValidator is used as for multiple text strings, each string should individually be checked + /// against the configured regular expression, rather than the JSON representing all the strings as a whole. + /// + public override IValueFormatValidator FormatValidator => new MultipleTextStringFormatValidator(); + } + + internal class MultipleTextStringFormatValidator : IValueFormatValidator + { + public IEnumerable ValidateFormat(object value, string valueType, string format) + { + var asArray = value as JArray; + if (asArray == null) + { + return Enumerable.Empty(); + } + + var textStrings = asArray.OfType() + .Where(x => x["value"] != null) + .Select(x => x["value"].Value()); + var textStringValidator = new RegexValidator(); + foreach (var textString in textStrings) + { + var validationResults = textStringValidator.ValidateFormat(textString, valueType, format).ToList(); + if (validationResults.Any()) + { + return validationResults; + } + } + + return Enumerable.Empty(); + } } } }