Conflicts: src/Umbraco.Core/Configuration/ClientDependencyConfiguration.cs src/Umbraco.Core/Constants-Conventions.cs src/Umbraco.Core/Models/Member.cs src/Umbraco.Core/Models/Membership/IUser.cs src/Umbraco.Core/Models/Template.cs src/Umbraco.Core/Persistence/Factories/MemberReadOnlyFactory.cs src/Umbraco.Core/Persistence/Repositories/ContentRepository.cs src/Umbraco.Core/Persistence/Repositories/Interfaces/IMemberRepository.cs src/Umbraco.Core/Persistence/Repositories/Interfaces/IUserRepository.cs src/Umbraco.Core/Persistence/Repositories/MemberGroupRepository.cs src/Umbraco.Core/Persistence/Repositories/MemberRepository.cs src/Umbraco.Core/Persistence/Repositories/PermissionRepository.cs src/Umbraco.Core/Persistence/RepositoryFactory.cs src/Umbraco.Core/Services/ContentService.cs src/Umbraco.Core/Services/IMemberTypeService.cs src/Umbraco.Core/Services/IMembershipUserService.cs src/Umbraco.Core/Services/IUserService.cs src/Umbraco.Core/Services/MemberService.cs src/Umbraco.Core/Services/MemberTypeService.cs src/Umbraco.Core/Services/ServiceContext.cs src/Umbraco.Core/Services/UserService.cs src/Umbraco.Core/Umbraco.Core.csproj src/Umbraco.Tests/MockTests.cs src/Umbraco.Tests/Persistence/Repositories/ContentRepositoryTest.cs src/Umbraco.Tests/Persistence/Repositories/ContentTypeRepositoryTest.cs src/Umbraco.Tests/Persistence/Repositories/MemberRepositoryTest.cs src/Umbraco.Tests/Persistence/Repositories/UserRepositoryTest.cs src/Umbraco.Web.UI/Umbraco.Web.UI.csproj src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/EditProfile.cshtml src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/Login.cshtml src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/LoginStatus.cshtml src/Umbraco.Web.UI/Umbraco/PartialViewMacros/Templates/RegisterMember.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/Breadcrumb.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/EditProfile.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/ListAncestorsFromCurrentPage.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/ListChildPagesFromCurrentPage.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/ListChildPagesOrderedByDate.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/ListChildPagesOrderedByName.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/ListChildPagesWithDoctype.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/ListDescendantsFromCurrentPage.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/Login.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/LoginStatus.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/MultinodeTree-picker.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/Navigation.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/RegisterMember.cshtml src/Umbraco.Web.UI/Umbraco/PartialViews/Templates/SiteMap.cshtml src/Umbraco.Web.UI/Umbraco/create/PartialView.ascx.cs src/Umbraco.Web.UI/config/umbracoSettings.config src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs src/Umbraco.Web/Controllers/UmbLoginController.cs src/Umbraco.Web/Controllers/UmbLoginStatusController.cs src/Umbraco.Web/Controllers/UmbProfileController.cs src/Umbraco.Web/Controllers/UmbRegisterController.cs src/Umbraco.Web/Models/LoginModel.cs src/Umbraco.Web/Models/LoginStatusModel.cs src/Umbraco.Web/Models/ProfileModel.cs src/Umbraco.Web/Models/RegisterModel.cs src/Umbraco.Web/Models/UmbracoProperty.cs src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs src/Umbraco.Web/PublishedCache/XmlPublishedCache/PublishedMediaCache.cs src/Umbraco.Web/Security/WebSecurity.cs src/Umbraco.Web/Umbraco.Web.csproj src/Umbraco.Web/UmbracoModule.cs src/Umbraco.Web/WebApi/UmbracoApiController.cs src/Umbraco.Web/umbraco.presentation/library.cs src/Umbraco.Web/umbraco.presentation/umbraco/controls/ContentTypeControlNew.ascx.cs src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewMacrosTasks.cs src/Umbraco.Web/umbraco.presentation/umbraco/create/PartialViewTasks.cs src/Umbraco.Web/umbraco.presentation/umbraco/editContent.aspx.cs src/Umbraco.Web/umbraco.presentation/umbraco/login.aspx.cs src/Umbraco.Web/umbraco.presentation/umbraco/members/EditMember.aspx.cs src/Umbraco.Web/umbraco.presentation/umbraco/users/EditUser.aspx.cs src/Umbraco.Web/umbraco.presentation/umbracobase/baseLibrary.cs src/Umbraco.Web/umbraco.presentation/umbracobase/restExtension.cs src/umbraco.businesslogic/User.cs src/umbraco.providers/UsersMembershipProvider.cs src/umbraco.providers/members/UmbracoMembershipProvider.cs
122 lines
4.7 KiB
C#
122 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Web.Mvc;
|
|
|
|
namespace Umbraco.Web
|
|
{
|
|
internal static class ModelStateExtensions
|
|
{
|
|
|
|
/// <summary>
|
|
/// Merges ModelState that has names matching the prefix
|
|
/// </summary>
|
|
/// <param name="state"></param>
|
|
/// <param name="dictionary"></param>
|
|
/// <param name="prefix"></param>
|
|
public static void Merge(this ModelStateDictionary state, ModelStateDictionary dictionary, string prefix)
|
|
{
|
|
if (dictionary == null)
|
|
return;
|
|
foreach (var keyValuePair in dictionary
|
|
//It can either equal the prefix exactly (model level errors) or start with the prefix. (property level errors)
|
|
.Where(keyValuePair => keyValuePair.Key == prefix || keyValuePair.Key.StartsWith(prefix + ".")))
|
|
{
|
|
state[keyValuePair.Key] = keyValuePair.Value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if there are any model errors on any fields containing the prefix
|
|
/// </summary>
|
|
/// <param name="state"></param>
|
|
/// <param name="prefix"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValid(this ModelStateDictionary state, string prefix)
|
|
{
|
|
return state.Where(v => v.Key.StartsWith(prefix + ".")).All(v => !v.Value.Errors.Any());
|
|
}
|
|
|
|
|
|
//NOTE: we used this alot in v5 when we had editors in MVC, this was really handy for knockout editors using JS
|
|
|
|
///// <summary>
|
|
///// Adds an error to the model state that has to do with data validation, this is generally used for JSON responses
|
|
///// </summary>
|
|
///// <param name="state"></param>
|
|
///// <param name="errorMessage"></param>
|
|
//public static void AddDataValidationError(this ModelStateDictionary state, string errorMessage)
|
|
//{
|
|
// state.AddModelError("DataValidation", errorMessage);
|
|
//}
|
|
|
|
/// <summary>
|
|
/// Adds the error to model state correctly for a property so we can use it on the client side.
|
|
/// </summary>
|
|
/// <param name="modelState"></param>
|
|
/// <param name="result"></param>
|
|
/// <param name="propertyAlias"></param>
|
|
internal static void AddPropertyError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, ValidationResult result, string propertyAlias)
|
|
{
|
|
//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
|
|
if (!result.MemberNames.Any())
|
|
{
|
|
//add a model state error for the entire property
|
|
modelState.AddModelError(string.Format("{0}.{1}", "Properties", propertyAlias), result.ErrorMessage);
|
|
}
|
|
else
|
|
{
|
|
//there's assigned field names so we'll combine the field name with the property name
|
|
// so that we can try to match it up to a real sub field of this editor
|
|
foreach (var field in result.MemberNames)
|
|
{
|
|
modelState.AddModelError(string.Format("{0}.{1}.{2}", "Properties", propertyAlias, field), result.ErrorMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IDictionary<string, object> ToErrorDictionary(this System.Web.Http.ModelBinding.ModelStateDictionary modelState)
|
|
{
|
|
var modelStateError = new Dictionary<string, object>();
|
|
foreach (var keyModelStatePair in modelState)
|
|
{
|
|
var key = keyModelStatePair.Key;
|
|
var errors = keyModelStatePair.Value.Errors;
|
|
if (errors != null && errors.Count > 0)
|
|
{
|
|
modelStateError.Add(key, errors.Select(error => error.ErrorMessage));
|
|
}
|
|
}
|
|
return modelStateError;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serializes the ModelState to JSON for JavaScript to interrogate the errors
|
|
/// </summary>
|
|
/// <param name="state"></param>
|
|
/// <returns></returns>
|
|
public static JsonResult ToJsonErrors(this ModelStateDictionary state)
|
|
{
|
|
return new JsonResult
|
|
{
|
|
Data = new
|
|
{
|
|
success = state.IsValid.ToString().ToLower(),
|
|
failureType = "ValidationError",
|
|
validationErrors = from e in state
|
|
where e.Value.Errors.Count > 0
|
|
select new
|
|
{
|
|
name = e.Key,
|
|
errors = e.Value.Errors.Select(x => x.ErrorMessage)
|
|
.Concat(
|
|
e.Value.Errors.Where(x => x.Exception != null).Select(x => x.Exception.Message))
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
}
|
|
} |