Validation errors reworked to incorporate segment errors (#8065)

This commit is contained in:
Daniël Knippers
2020-05-14 17:03:33 +02:00
committed by GitHub
parent 945f4db4c3
commit 0fdf084544
6 changed files with 141 additions and 87 deletions

View File

@@ -65,11 +65,12 @@ namespace Umbraco.Web
/// </summary>
/// <param name="modelState"></param>
/// <param name="culture"></param>
/// <param name="segment"></param>
/// <param name="errMsg"></param>
internal static void AddCultureValidationError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
string culture, string errMsg)
internal static void AddVariantValidationError(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
string culture, string segment, string errMsg)
{
var key = "_content_variant_" + culture + "_";
var key = "_content_variant_" + (culture.IsNullOrWhiteSpace() ? "invariant" : culture) + "_" + (segment.IsNullOrWhiteSpace() ? "null" : segment) + "_";
if (modelState.ContainsKey(key)) return;
modelState.AddModelError(key, errMsg);
}
@@ -83,23 +84,28 @@ namespace Umbraco.Web
/// <returns>
/// A list of cultures that have property validation errors. The default culture will be returned for any invariant property errors.
/// </returns>
internal static IReadOnlyList<string> GetCulturesWithPropertyErrors(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
ILocalizationService localizationService, string cultureForInvariantErrors)
internal static IReadOnlyList<(string culture, string segment)> GetVariantsWithPropertyErrors(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
string cultureForInvariantErrors)
{
//Add any culture specific errors here
var cultureErrors = modelState.Keys
//Add any variant specific errors here
var variantErrors = modelState.Keys
.Where(key => key.StartsWith("_Properties.")) //only choose _Properties errors
.Select(x => x.Split('.')) //split into parts
.Where(x => x.Length >= 3 && x[0] == "_Properties") //only choose _Properties errors
.Select(x => x[2]) //select the culture part
.Where(x => !x.IsNullOrWhiteSpace()) //if it has a value
//if it's marked "invariant" than return the default language, this is because we can only edit invariant properties on the default language
.Where(x => x.Length >= 4 && !x[2].IsNullOrWhiteSpace() && !x[3].IsNullOrWhiteSpace())
.Select(x => (culture: x[2], segment: x[3]))
//if the culture is marked "invariant" than return the default language, this is because we can only edit invariant properties on the default language
//so errors for those must show up under the default lang.
.Select(x => x == "invariant" ? cultureForInvariantErrors : x)
.WhereNotNull()
//if the segment is marked "null" then return an actual null
.Select(x =>
{
var culture = x.culture == "invariant" ? cultureForInvariantErrors : x.culture;
var segment = x.segment == "null" ? null : x.segment;
return (culture, segment);
})
.Distinct()
.ToList();
return cultureErrors;
return variantErrors;
}
/// <summary>
@@ -111,23 +117,33 @@ namespace Umbraco.Web
/// <returns>
/// A list of cultures that have validation errors. The default culture will be returned for any invariant errors.
/// </returns>
internal static IReadOnlyList<string> GetCulturesWithErrors(this System.Web.Http.ModelBinding.ModelStateDictionary modelState,
ILocalizationService localizationService, string cultureForInvariantErrors)
internal static IReadOnlyList<(string culture, string segment)> GetVariantsWithErrors(this System.Web.Http.ModelBinding.ModelStateDictionary modelState, string cultureForInvariantErrors)
{
var propertyCultureErrors = modelState.GetCulturesWithPropertyErrors(localizationService, cultureForInvariantErrors);
var propertyVariantErrors = modelState.GetVariantsWithPropertyErrors(cultureForInvariantErrors);
//now check the other special culture errors that are
var genericCultureErrors = modelState.Keys
//now check the other special variant errors that are
var genericVariantErrors = modelState.Keys
.Where(x => x.StartsWith("_content_variant_") && x.EndsWith("_"))
.Select(x => x.TrimStart("_content_variant_").TrimEnd("_"))
.Where(x => !x.IsNullOrWhiteSpace())
.Select(x => x.TrimStart("_content_variant_").TrimEnd("_"))
.Select(x =>
{
// Format "<culture>_<segment>"
var cs = x.Split(new[] { '_' });
return (culture: cs[0], segment: cs[1]);
})
.Where(x => !x.culture.IsNullOrWhiteSpace())
//if it's marked "invariant" than return the default language, this is because we can only edit invariant properties on the default language
//so errors for those must show up under the default lang.
.Select(x => x == "invariant" ? cultureForInvariantErrors : x)
.WhereNotNull()
//if the segment is marked "null" then return an actual null
.Select(x =>
{
var culture = x.culture == "invariant" ? cultureForInvariantErrors : x.culture;
var segment = x.segment == "null" ? null : x.segment;
return (culture, segment);
})
.Distinct();
return propertyCultureErrors.Union(genericCultureErrors).ToList();
return propertyVariantErrors.Union(genericVariantErrors).Distinct().ToList();
}
/// <summary>