diff --git a/src/Umbraco.Web/Editors/ContentTypeControllerBase.cs b/src/Umbraco.Web/Editors/ContentTypeControllerBase.cs index 2c6d66bdd7..de7835e0d5 100644 --- a/src/Umbraco.Web/Editors/ContentTypeControllerBase.cs +++ b/src/Umbraco.Web/Editors/ContentTypeControllerBase.cs @@ -206,7 +206,7 @@ namespace Umbraco.Web.Editors } //now let the external validators execute - EditorValidationResolver.Current.Validate(contentTypeSave, new EditorValidationErrors((key, msg) => ModelState.AddModelError(key, msg))); + ValidationHelper.ValidateEditorModelWithResolver(ModelState, contentTypeSave); if (ModelState.IsValid == false) { diff --git a/src/Umbraco.Web/Editors/EditorValidationErrors.cs b/src/Umbraco.Web/Editors/EditorValidationErrors.cs deleted file mode 100644 index 95e5d8590a..0000000000 --- a/src/Umbraco.Web/Editors/EditorValidationErrors.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -namespace Umbraco.Web.Editors -{ - /// - /// Used to encapsulate ModelStateDictionary, but because there are 2x this (mvc and webapi), this - /// is just a simple way to have one object for both - /// - internal class EditorValidationErrors - { - private readonly Action _addModelError; - - public EditorValidationErrors(Action addModelError) - { - _addModelError = addModelError; - } - - public void AddModelError(string key, string message) - { - _addModelError(key, message); - } - } -} \ No newline at end of file diff --git a/src/Umbraco.Web/Editors/EditorValidationResolver.cs b/src/Umbraco.Web/Editors/EditorValidationResolver.cs index c4c5902518..d0c5edf5af 100644 --- a/src/Umbraco.Web/Editors/EditorValidationResolver.cs +++ b/src/Umbraco.Web/Editors/EditorValidationResolver.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; +using Umbraco.Core; using Umbraco.Core.Logging; using Umbraco.Core.ObjectResolution; @@ -18,12 +20,12 @@ namespace Umbraco.Web.Editors get { return Values; } } - public void Validate(object model, EditorValidationErrors editorValidations) + public IEnumerable Validate(object model) { - foreach (var validator in EditorValidators.Where(x => x.GetType() == x.ModelType)) - { - validator.Validate(model, editorValidations); - } + return EditorValidators + .Where(x => model.GetType() == x.ModelType) + .WhereNotNull() + .SelectMany(x => x.Validate(model)); } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Editors/EditorValidator.cs b/src/Umbraco.Web/Editors/EditorValidator.cs index 21698bd53f..d123838f52 100644 --- a/src/Umbraco.Web/Editors/EditorValidator.cs +++ b/src/Umbraco.Web/Editors/EditorValidator.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace Umbraco.Web.Editors { @@ -9,6 +11,11 @@ namespace Umbraco.Web.Editors get { return typeof (T); } } - public abstract void Validate(object model, EditorValidationErrors editorValidations); + protected abstract IEnumerable PerformValidate(T model); + + public IEnumerable Validate(object model) + { + return PerformValidate((T) model); + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Editors/IEditorValidator.cs b/src/Umbraco.Web/Editors/IEditorValidator.cs index 318564b1d9..e1d4e68ed2 100644 --- a/src/Umbraco.Web/Editors/IEditorValidator.cs +++ b/src/Umbraco.Web/Editors/IEditorValidator.cs @@ -1,10 +1,12 @@ using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace Umbraco.Web.Editors { internal interface IEditorValidator { Type ModelType { get; } - void Validate(object model, EditorValidationErrors editorValidations); + IEnumerable Validate(object model); } } \ No newline at end of file diff --git a/src/Umbraco.Web/Editors/ValidationHelper.cs b/src/Umbraco.Web/Editors/ValidationHelper.cs index 06699c56af..e1633fa6bf 100644 --- a/src/Umbraco.Web/Editors/ValidationHelper.cs +++ b/src/Umbraco.Web/Editors/ValidationHelper.cs @@ -3,6 +3,7 @@ using System.ComponentModel; using System.Linq; using System.Web; using System.Web.Http.ModelBinding; +using Umbraco.Core; using Umbraco.Core.Models.Validation; using Umbraco.Web.Models.ContentEditing; @@ -10,6 +11,21 @@ namespace Umbraco.Web.Editors { internal class ValidationHelper { + internal static void ValidateEditorModelWithResolver(ModelStateDictionary modelState, object model) + { + var validationResult = EditorValidationResolver.Current.Validate(model); + foreach (var vr in validationResult + .WhereNotNull() + .Where(x => x.ErrorMessage.IsNullOrWhiteSpace() == false) + .Where(x => x.MemberNames.Any())) + { + foreach (var memberName in vr.MemberNames) + { + modelState.AddModelError(memberName, vr.ErrorMessage); + } + } + } + /// /// This will check if any properties of the model are attributed with the RequiredForPersistenceAttribute attribute and if they are it will /// check if that property validates, if it doesn't it means that the current model cannot be persisted because it doesn't have the necessary information diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index b9d0be3dec..67af0ee515 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -303,7 +303,6 @@ -