forked from mcintyre321/FormFactory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAspMvcValidationHelper.cs
More file actions
96 lines (77 loc) · 3.7 KB
/
Copy pathAspMvcValidationHelper.cs
File metadata and controls
96 lines (77 loc) · 3.7 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections;
using System.Collections.Generic;
using FormFactory.Attributes;
using System.Linq;
using System.Reflection;
using System.Text;
using FormFactory.AspMvc.Wrappers;
using FormFactory.UnobtrusiveValidation;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace FormFactory
{
public static class AspMvcValidationHelper
{
public static IHtmlContent AllValidationMessages(this IHtmlHelper helper, string modelName)
{
if (HasErrors(helper, modelName))
{
var message = string.Join(", ", helper.ViewData.ModelState[modelName].Errors.Select(e => e.ErrorMessage));
return new HtmlString(message);
}
return new HtmlString("");
}
public static bool HasErrors(this IHtmlHelper helper, string modelName)
{
return helper.ViewData.ModelState.ContainsKey(modelName) &&
helper.ViewData.ModelState[modelName].Errors.Count > 0;
}
public static List<UnobtrusiveValidationRule> UnobtrusiveValidationRules = new List<UnobtrusiveValidationRule>()
{
GetRulesFromAttributes
};
private static IEnumerable<ModelClientValidationRule> GetRulesFromAttributes(PropertyVm property)
{
return property.GetCustomAttributes()
.SelectMany(attribute => UnobtrusiveValidationAttributeRules,
(attribute, rule) => rule(property, attribute))
.Where(r => r != null);
}
public static List<UnobtrusiveValidationAttributeRule> UnobtrusiveValidationAttributeRules = new List
<UnobtrusiveValidationAttributeRule>()
{
RangeAttribteRule,
RequiredAttributeRule,
StringLengthAttributeRule,
RegexAttributeRule
};
private static ModelClientValidationRule RangeAttribteRule(PropertyVm propertyVm, object attribute)
{
var a = attribute as RangeAttribute;
return (a == null) ? null : new ModelClientValidationRangeRule(a.FormatErrorMessage(propertyVm.DisplayName), a.Minimum, a.Maximum);
}
private static ModelClientValidationRule RequiredAttributeRule(PropertyVm propertyVm, object attribute)
{
var a = attribute as RequiredAttribute ;
return (a == null) ? null : new ModelClientValidationRequiredRule(a.FormatErrorMessage(propertyVm.DisplayName));
}
private static ModelClientValidationRule StringLengthAttributeRule(PropertyVm propertyVm, object attribute)
{
var a = attribute as StringLengthAttribute;
return (a == null) ? null : new ModelClientValidationStringLengthRule(a.FormatErrorMessage(propertyVm.DisplayName), a.MinimumLength, a.MaximumLength);
}
private static ModelClientValidationRule RegexAttributeRule(PropertyVm propertyVm, object attribute)
{
var a = attribute as RegularExpressionAttribute;
return (a == null) ? null : new ModelClientValidationRegexRule(a.FormatErrorMessage(propertyVm.DisplayName), a.Pattern);
}
public delegate IEnumerable<ModelClientValidationRule> UnobtrusiveValidationRule(PropertyVm property);
public delegate ModelClientValidationRule UnobtrusiveValidationAttributeRule(PropertyVm property, object attribute);
public static IHtmlContent UnobtrusiveValidation(this IHtmlHelper helper, PropertyVm property)
{
var unobtrusiveValidation = ValidationHelper.UnobtrusiveValidation(new FormFactoryHtmlHelper(helper), property);
return new HtmlString(unobtrusiveValidation);
}
}
}