forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlHelper.Validation.cs
More file actions
185 lines (163 loc) · 7.63 KB
/
HtmlHelper.Validation.cs
File metadata and controls
185 lines (163 loc) · 7.63 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.Internal.Web.Utils;
namespace System.Web.WebPages.Html
{
public partial class HtmlHelper
{
public IHtmlString ValidationMessage(string name)
{
return ValidationMessage(name, null, null);
}
public IHtmlString ValidationMessage(string name, string message)
{
return ValidationMessage(name, message, (IDictionary<string, object>)null);
}
public IHtmlString ValidationMessage(string name, object htmlAttributes)
{
return ValidationMessage(name, null, AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString ValidationMessage(string name, IDictionary<string, object> htmlAttributes)
{
return ValidationMessage(name, null, htmlAttributes);
}
public IHtmlString ValidationMessage(string name, string message, object htmlAttributes)
{
return ValidationMessage(name, message, AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString ValidationMessage(string name, string message, IDictionary<string, object> htmlAttributes)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "name");
}
return BuildValidationMessage(name, message, htmlAttributes);
}
[SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase",
Justification = "Normalization to lowercase is a common requirement for JavaScript and HTML values")]
private IHtmlString BuildValidationMessage(string name, string message, IDictionary<string, object> htmlAttributes)
{
var modelState = ModelState[name];
IEnumerable<string> errors = null;
if (modelState != null)
{
errors = modelState.Errors;
}
bool hasError = errors != null && errors.Any();
if (!hasError && !UnobtrusiveJavaScriptEnabled)
{
// If unobtrusive validation is enabled, we need to generate an empty span with the "val-for" attribute"
return null;
}
else
{
string error = null;
if (hasError)
{
error = message ?? errors.First();
}
TagBuilder tagBuilder = new TagBuilder("span") { InnerHtml = Encode(error) };
tagBuilder.MergeAttributes(htmlAttributes);
if (UnobtrusiveJavaScriptEnabled)
{
bool replaceValidationMessageContents = String.IsNullOrEmpty(message);
tagBuilder.MergeAttribute("data-valmsg-for", name);
tagBuilder.MergeAttribute("data-valmsg-replace", replaceValidationMessageContents.ToString().ToLowerInvariant());
}
tagBuilder.AddCssClass(hasError ? ValidationMessageCssClassName : ValidationMessageValidCssClassName);
return tagBuilder.ToHtmlString(TagRenderMode.Normal);
}
}
public IHtmlString ValidationSummary()
{
return BuildValidationSummary(message: null, excludeFieldErrors: false, htmlAttributes: (IDictionary<string, object>)null);
}
public IHtmlString ValidationSummary(string message)
{
return BuildValidationSummary(message: message, excludeFieldErrors: false, htmlAttributes: (IDictionary<string, object>)null);
}
public IHtmlString ValidationSummary(bool excludeFieldErrors)
{
return ValidationSummary(message: null, excludeFieldErrors: excludeFieldErrors, htmlAttributes: (IDictionary<string, object>)null);
}
public IHtmlString ValidationSummary(object htmlAttributes)
{
return ValidationSummary(message: null, excludeFieldErrors: false, htmlAttributes: htmlAttributes);
}
public IHtmlString ValidationSummary(IDictionary<string, object> htmlAttributes)
{
return ValidationSummary(message: null, excludeFieldErrors: false, htmlAttributes: htmlAttributes);
}
public IHtmlString ValidationSummary(string message, object htmlAttributes)
{
return ValidationSummary(message, excludeFieldErrors: false, htmlAttributes: htmlAttributes);
}
public IHtmlString ValidationSummary(string message, IDictionary<string, object> htmlAttributes)
{
return ValidationSummary(message, excludeFieldErrors: false, htmlAttributes: htmlAttributes);
}
public IHtmlString ValidationSummary(string message, bool excludeFieldErrors, object htmlAttributes)
{
return ValidationSummary(message, excludeFieldErrors, AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public IHtmlString ValidationSummary(string message, bool excludeFieldErrors, IDictionary<string, object> htmlAttributes)
{
return BuildValidationSummary(message, excludeFieldErrors, htmlAttributes);
}
private IHtmlString BuildValidationSummary(string message, bool excludeFieldErrors, IDictionary<string, object> htmlAttributes)
{
IEnumerable<string> errors = null;
if (excludeFieldErrors)
{
// Review: Is there a better way to share the form field name between this and ModelStateDictionary?
var formModelState = ModelState[ModelStateDictionary.FormFieldKey];
if (formModelState != null)
{
errors = formModelState.Errors;
}
}
else
{
errors = ModelState.SelectMany(c => c.Value.Errors);
}
bool hasErrors = errors != null && errors.Any();
if (!hasErrors && (!UnobtrusiveJavaScriptEnabled || excludeFieldErrors))
{
// If no errors are found and we do not have unobtrusive validation enabled or if the summary is not meant to display field errors, don't generate the summary.
return null;
}
else
{
TagBuilder tagBuilder = new TagBuilder("div");
tagBuilder.MergeAttributes(htmlAttributes);
tagBuilder.AddCssClass(hasErrors ? ValidationSummaryClass : ValidationSummaryValidClass);
if (UnobtrusiveJavaScriptEnabled && !excludeFieldErrors)
{
tagBuilder.MergeAttribute("data-valmsg-summary", "true");
}
StringBuilder builder = new StringBuilder();
if (message != null)
{
builder.Append("<span>");
builder.Append(Encode(message));
builder.AppendLine("</span>");
}
builder.AppendLine("<ul>");
foreach (var error in errors)
{
builder.Append("<li>");
builder.Append(Encode(error));
builder.AppendLine("</li>");
}
builder.Append("</ul>");
tagBuilder.InnerHtml = builder.ToString();
return tagBuilder.ToHtmlString(TagRenderMode.Normal);
}
}
}
}