* Run code cleanup * Run dotnet format * Start manual cleanup in Web.Common * Finish up manual cleanup * Fix tests * Fix up InMemoryModelFactory.cs * Inject proper macroRenderer * Update src/Umbraco.Web.Common/Filters/JsonDateTimeFormatAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Web.Common/Filters/ValidateUmbracoFormRouteStringAttribute.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix based on review Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch>
54 lines
2.0 KiB
C#
54 lines
2.0 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
namespace Umbraco.Extensions;
|
|
|
|
public static class ModelStateExtensions
|
|
{
|
|
/// <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) =>
|
|
state.Where(v => v.Key.StartsWith(prefix + ".")).All(v => !v.Value?.Errors.Any() ?? false);
|
|
|
|
public static IDictionary<string, object> ToErrorDictionary(this ModelStateDictionary modelState)
|
|
{
|
|
var modelStateError = new Dictionary<string, object>();
|
|
foreach (KeyValuePair<string, ModelStateEntry> keyModelStatePair in modelState)
|
|
{
|
|
var key = keyModelStatePair.Key;
|
|
ModelErrorCollection errors = keyModelStatePair.Value.Errors;
|
|
if (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) =>
|
|
new(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)),
|
|
},
|
|
});
|
|
}
|