forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationHelper.cs
More file actions
295 lines (258 loc) · 10.7 KB
/
Copy pathValidationHelper.cs
File metadata and controls
295 lines (258 loc) · 10.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// 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.ComponentModel.DataAnnotations;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.WebPages.Html;
using System.Web.WebPages.Scope;
using Microsoft.Internal.Web.Utils;
namespace System.Web.WebPages
{
public sealed class ValidationHelper
{
private static readonly object _invalidCssClassKey = new object();
private static readonly object _validCssClassKey = new object();
private static IDictionary<object, object> _scopeOverride;
private readonly Dictionary<string, List<IValidator>> _validators = new Dictionary<string, List<IValidator>>(StringComparer.OrdinalIgnoreCase);
private readonly HttpContextBase _httpContext;
private readonly ModelStateDictionary _modelStateDictionary;
internal ValidationHelper(HttpContextBase httpContext, ModelStateDictionary modelStateDictionary)
{
Debug.Assert(httpContext != null);
Debug.Assert(modelStateDictionary != null);
_httpContext = httpContext;
_modelStateDictionary = modelStateDictionary;
}
public static string ValidCssClass
{
get
{
object value;
if (!Scope.TryGetValue(_validCssClassKey, out value))
{
return null;
}
return value as string;
}
set { Scope[_validCssClassKey] = value; }
}
public static string InvalidCssClass
{
get
{
object value;
if (!Scope.TryGetValue(_invalidCssClassKey, out value))
{
return HtmlHelper.DefaultValidationInputErrorCssClass;
}
return value as string;
}
set { Scope[_invalidCssClassKey] = value; }
}
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This makes it easier for a user to read this value without knowing of this type.")]
public string FormField
{
get { return ModelStateDictionary.FormFieldKey; }
}
internal static IDictionary<object, object> Scope
{
get { return _scopeOverride ?? ScopeStorage.CurrentScope; }
}
public void RequireField(string field)
{
RequireField(field, errorMessage: null);
}
public void RequireField(string field, string errorMessage)
{
if (String.IsNullOrEmpty(field))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "field");
}
Add(field, Validator.Required(errorMessage: errorMessage));
}
public void RequireFields(params string[] fields)
{
if (fields == null)
{
throw new ArgumentNullException("fields");
}
foreach (var field in fields)
{
RequireField(field);
}
}
public void Add(string field, params IValidator[] validators)
{
if (String.IsNullOrEmpty(field))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "field");
}
if ((validators == null) || validators.Any(v => v == null))
{
throw new ArgumentNullException("validators");
}
AddFieldValidators(field, validators);
}
public void Add(IEnumerable<string> fields, params IValidator[] validators)
{
if (fields == null)
{
throw new ArgumentNullException("fields");
}
if (validators == null)
{
throw new ArgumentNullException("validators");
}
foreach (var field in fields)
{
Add(field, validators);
}
}
public void AddFormError(string errorMessage)
{
_modelStateDictionary.AddFormError(errorMessage);
}
public bool IsValid(params string[] fields)
{
// Don't need to validate fields as we treat empty fields as all in Validate.
return !Validate(fields).Any();
}
public IEnumerable<ValidationResult> Validate(params string[] fields)
{
IEnumerable<string> keys = fields;
if (fields == null || !fields.Any())
{
// If no fields are present, validate all of them.
keys = _validators.Keys.Concat(new[] { FormField });
}
return ValidateFieldsAndUpdateModelState(keys);
}
public IEnumerable<string> GetErrors(params string[] fields)
{
// Don't need to validate fields as we treat empty fields as all in Validate.
return Validate(fields).Select(r => r.ErrorMessage);
}
public HtmlString For(string field)
{
if (String.IsNullOrEmpty(field))
{
throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "field");
}
var clientRules = GetClientValidationRules(field);
return GenerateHtmlFromClientValidationRules(clientRules);
}
public HtmlString ClassFor(string field)
{
if (_httpContext != null && String.Equals("POST", _httpContext.Request.HttpMethod, StringComparison.OrdinalIgnoreCase))
{
string cssClass = IsValid(field) ? ValidationHelper.ValidCssClass : ValidationHelper.InvalidCssClass;
return cssClass == null ? null : new HtmlString(cssClass);
}
return null;
}
internal static IDisposable OverrideScope()
{
_scopeOverride = new Dictionary<object, object>();
return new DisposableAction(() => _scopeOverride = null);
}
internal IDictionary<string, object> GetUnobtrusiveValidationAttributes(string field)
{
var clientRules = GetClientValidationRules(field);
var attributes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules, attributes);
return attributes;
}
private IEnumerable<ValidationResult> ValidateFieldsAndUpdateModelState(IEnumerable<string> fields)
{
var validationContext = new ValidationContext(_httpContext, serviceProvider: null, items: null);
var validationResults = new List<ValidationResult>();
foreach (var field in fields)
{
IEnumerable<ValidationResult> fieldResults = ValidateField(field, validationContext);
IEnumerable<string> errors = fieldResults.Select(c => c.ErrorMessage);
ModelState modelState = _modelStateDictionary[field];
if (modelState != null && modelState.Errors.Any())
{
errors = errors.Except(modelState.Errors, StringComparer.OrdinalIgnoreCase);
// If there were other validation errors that were added via ModelState, add them to the collection.
fieldResults = fieldResults.Concat(modelState.Errors.Select(e => new ValidationResult(e, new[] { field })));
}
foreach (var errorMessage in errors)
{
// Only add errors that haven't been encountered before. This is to prevent from the same error message being duplicated
// if a call is made multiple times
_modelStateDictionary.AddError(field, errorMessage);
}
validationResults.AddRange(fieldResults);
}
return validationResults;
}
private void AddFieldValidators(string field, params IValidator[] validators)
{
List<IValidator> fieldValidators = null;
if (!_validators.TryGetValue(field, out fieldValidators))
{
fieldValidators = new List<IValidator>();
_validators[field] = fieldValidators;
}
foreach (var validator in validators)
{
fieldValidators.Add(validator);
}
}
private IEnumerable<ValidationResult> ValidateField(string field, ValidationContext context)
{
List<IValidator> fieldValidators;
if (!_validators.TryGetValue(field, out fieldValidators))
{
return Enumerable.Empty<ValidationResult>();
}
context.MemberName = field;
return fieldValidators.Select(f => f.Validate(context))
.Where(result => result != ValidationResult.Success);
}
private IEnumerable<ModelClientValidationRule> GetClientValidationRules(string field)
{
List<IValidator> fieldValidators = null;
if (!_validators.TryGetValue(field, out fieldValidators))
{
return Enumerable.Empty<ModelClientValidationRule>();
}
return from item in fieldValidators
let clientRule = item.ClientValidationRule
where clientRule != null
select clientRule;
}
internal static HtmlString GenerateHtmlFromClientValidationRules(IEnumerable<ModelClientValidationRule> clientRules)
{
if (!clientRules.Any())
{
return null;
}
var attributes = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(clientRules, attributes);
var stringBuilder = new StringBuilder();
foreach (var attribute in attributes)
{
string key = attribute.Key;
string value = HttpUtility.HtmlEncode(Convert.ToString(attribute.Value, CultureInfo.InvariantCulture));
stringBuilder.Append(key)
.Append("=\"")
.Append(value)
.Append('"')
.Append(' ');
}
// Trim trailing whitespace
if (stringBuilder.Length > 0)
{
stringBuilder.Length--;
}
return new HtmlString(stringBuilder.ToString());
}
}
}