-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathSummaryByPropertyValidatorHtml.cs
More file actions
47 lines (40 loc) · 1.47 KB
/
SummaryByPropertyValidatorHtml.cs
File metadata and controls
47 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Globalization;
using System.Text;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace CodelyTv.Apps.Backoffice.Frontend.Extension.Validators
{
public static class SummaryByPropertyValidatorHtml
{
public static IHtmlContent ValidationSummaryByProperty<TModel>(this IHtmlHelper<TModel> helper,
ModelStateDictionary dictionary, string property, string className)
{
if (helper == null)
{
throw new ArgumentNullException(nameof(helper));
}
if (dictionary == null)
{
throw new ArgumentNullException(nameof(dictionary));
}
if (string.IsNullOrEmpty(property))
{
throw new ArgumentNullException(nameof(property));
}
if (className == null)
{
throw new ArgumentNullException(nameof(className));
}
var builder = new StringBuilder();
if (dictionary.TryGetValue(property, out var modelStateEntry) && modelStateEntry?.Errors != null)
{
foreach (var modelState in modelStateEntry.Errors)
{
builder.Append(CultureInfo.CurrentCulture, $"<p class='{className}'>{modelState.ErrorMessage}</p>");
}
}
return new HtmlString(builder.ToString());
}
}
}