Updates how the editor validators work
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Web.Editors
|
||||
{
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
internal class EditorValidationErrors
|
||||
{
|
||||
private readonly Action<string, string> _addModelError;
|
||||
|
||||
public EditorValidationErrors(Action<string, string> addModelError)
|
||||
{
|
||||
_addModelError = addModelError;
|
||||
}
|
||||
|
||||
public void AddModelError(string key, string message)
|
||||
{
|
||||
_addModelError(key, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<ValidationResult> 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<ValidationResult> PerformValidate(T model);
|
||||
|
||||
public IEnumerable<ValidationResult> Validate(object model)
|
||||
{
|
||||
return PerformValidate((T) model);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<ValidationResult> Validate(object model);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
|
||||
@@ -303,7 +303,6 @@
|
||||
<Compile Include="AreaRegistrationContextExtensions.cs" />
|
||||
<Compile Include="BatchedDatabaseServerMessengerStartup.cs" />
|
||||
<Compile Include="Editors\BackOfficeNotificationsController.cs" />
|
||||
<Compile Include="Editors\EditorValidationErrors.cs" />
|
||||
<Compile Include="Editors\EditorValidationResolver.cs" />
|
||||
<Compile Include="Editors\EditorValidator.cs" />
|
||||
<Compile Include="Editors\IEditorValidator.cs" />
|
||||
|
||||
Reference in New Issue
Block a user