forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationExtensions.cs
More file actions
260 lines (215 loc) · 12.3 KB
/
Copy pathValidationExtensions.cs
File metadata and controls
260 lines (215 loc) · 12.3 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
#if !NETSTANDARD2_0
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
namespace ServiceStack.Html
{
public static class ValidationExtensions
{
private const string HiddenListItem = @"<li style=""display:none""></li>";
private static string _resourceClassKey;
public static string ResourceClassKey
{
get { return _resourceClassKey ?? String.Empty; }
set { _resourceClassKey = value; }
}
private static FieldValidationMetadata ApplyFieldValidationMetadata(HtmlHelper htmlHelper, ModelMetadata modelMetadata, string modelName)
{
FormContext formContext = htmlHelper.ViewContext.FormContext;
FieldValidationMetadata fieldMetadata = formContext.GetValidationMetadataForField(modelName, true /* createIfNotFound */);
// write rules to context object
//IEnumerable<ModelValidator> validators = ModelValidatorProviders.Providers.GetValidators(modelMetadata, htmlHelper.ViewContext);
//foreach (ModelClientValidationRule rule in validators.SelectMany(v => v.GetClientValidationRules()))
//{
// fieldMetadata.ValidationRules.Add(rule);
//}
return fieldMetadata;
}
private static string GetInvalidPropertyValueResource(HttpContextBase httpContext)
{
string resourceValue = null;
if (!String.IsNullOrEmpty(ResourceClassKey) && (httpContext != null))
{
// If the user specified a ResourceClassKey try to load the resource they specified.
// If the class key is invalid, an exception will be thrown.
// If the class key is valid but the resource is not found, it returns null, in which
// case it will fall back to the MVC default error message.
resourceValue = httpContext.GetGlobalResourceObject(ResourceClassKey, "InvalidPropertyValue", CultureInfo.CurrentUICulture) as string;
}
return resourceValue ?? MvcResources.Common_ValueNotValidForProperty;
}
private static string GetUserErrorMessageOrDefault(HttpContextBase httpContext, ModelError error, ModelState modelState)
{
if (!String.IsNullOrEmpty(error.ErrorMessage))
{
return error.ErrorMessage;
}
if (modelState == null)
{
return null;
}
string attemptedValue = (modelState.Value != null) ? modelState.Value.AttemptedValue : null;
return String.Format(CultureInfo.CurrentCulture, GetInvalidPropertyValueResource(httpContext), attemptedValue);
}
// Validate
public static void Validate(this HtmlHelper htmlHelper, string modelName)
{
if (modelName == null)
{
throw new ArgumentNullException("modelName");
}
ValidateHelper(htmlHelper,
ModelMetadata.FromStringExpression(modelName, htmlHelper.ViewContext.ViewData),
modelName);
}
public static void ValidateFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
ValidateHelper(htmlHelper,
ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData),
ExpressionHelper.GetExpressionText(expression));
}
private static void ValidateHelper(HtmlHelper htmlHelper, ModelMetadata modelMetadata, string expression)
{
FormContext formContext = htmlHelper.ViewContext.GetFormContextForClientValidation();
if (formContext == null || htmlHelper.ViewContext.UnobtrusiveJavaScriptEnabled)
{
return; // nothing to do
}
string modelName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(expression);
ApplyFieldValidationMetadata(htmlHelper, modelMetadata, modelName);
}
// ValidationMessage
public static MvcHtmlString ValidationMessage(this HtmlHelper htmlHelper, string modelName)
{
return ValidationMessage(htmlHelper, modelName, null /* validationMessage */, new RouteValueDictionary());
}
public static MvcHtmlString ValidationMessage(this HtmlHelper htmlHelper, string modelName, object htmlAttributes)
{
return ValidationMessage(htmlHelper, modelName, null /* validationMessage */, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString ValidationMessage(this HtmlHelper htmlHelper, string modelName, string validationMessage)
{
return ValidationMessage(htmlHelper, modelName, validationMessage, new RouteValueDictionary());
}
public static MvcHtmlString ValidationMessage(this HtmlHelper htmlHelper, string modelName, string validationMessage, object htmlAttributes)
{
return ValidationMessage(htmlHelper, modelName, validationMessage, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString ValidationMessage(this HtmlHelper htmlHelper, string modelName, IDictionary<string, object> htmlAttributes)
{
return ValidationMessage(htmlHelper, modelName, null /* validationMessage */, htmlAttributes);
}
public static MvcHtmlString ValidationMessage(this HtmlHelper htmlHelper, string modelName, string validationMessage, IDictionary<string, object> htmlAttributes)
{
if (modelName == null)
{
throw new ArgumentNullException("modelName");
}
return ValidationMessageHelper(htmlHelper,
ModelMetadata.FromStringExpression(modelName, htmlHelper.ViewContext.ViewData),
modelName,
validationMessage,
htmlAttributes);
}
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
return ValidationMessageFor(htmlHelper, expression, null /* validationMessage */, new RouteValueDictionary());
}
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string validationMessage)
{
return ValidationMessageFor(htmlHelper, expression, validationMessage, new RouteValueDictionary());
}
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string validationMessage, object htmlAttributes)
{
return ValidationMessageFor(htmlHelper, expression, validationMessage, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString ValidationMessageFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string validationMessage, IDictionary<string, object> htmlAttributes)
{
return ValidationMessageHelper(htmlHelper,
ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData),
ExpressionHelper.GetExpressionText(expression),
validationMessage,
htmlAttributes);
}
private static MvcHtmlString ValidationMessageHelper(this HtmlHelper htmlHelper, ModelMetadata modelMetadata, string expression, string validationMessage, IDictionary<string, object> htmlAttributes)
{
string modelName = expression;
var fieldError = htmlHelper.GetFieldError(modelName);
if (fieldError == null)
return null;
var spanTag = new TagBuilder("span");
spanTag.MergeAttributes(htmlAttributes);
HtmlHelper.ValidationMessageCssClassNames.Split(' ').Each(spanTag.AddCssClass);
spanTag.AddCssClass(fieldError.ErrorCode);
spanTag.InnerHtml = fieldError.Message;
return spanTag.ToMvcHtmlString(TagRenderMode.Normal);
}
// ValidationSummary
public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper)
{
return ValidationSummary(htmlHelper, false /* excludePropertyErrors */);
}
public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors)
{
return ValidationSummary(htmlHelper, excludePropertyErrors, null /* message */);
}
public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, string message)
{
return ValidationSummary(htmlHelper, false /* excludePropertyErrors */, message, (object)null /* htmlAttributes */);
}
public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors, string message)
{
return ValidationSummary(htmlHelper, excludePropertyErrors, message, (object)null /* htmlAttributes */);
}
public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, string message, object htmlAttributes)
{
return ValidationSummary(htmlHelper, false /* excludePropertyErrors */, message, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors, string message, object htmlAttributes)
{
return ValidationSummary(htmlHelper, excludePropertyErrors, message, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, string message, IDictionary<string, object> htmlAttributes)
{
return ValidationSummary(htmlHelper, false /* excludePropertyErrors */, message, htmlAttributes);
}
public static MvcHtmlString ValidationSummary(this HtmlHelper htmlHelper, bool excludePropertyErrors, string message, IDictionary<string, object> htmlAttributes)
{
if (htmlHelper == null)
throw new ArgumentNullException("htmlHelper");
var errorStatus = htmlHelper.GetErrorStatus();
if (errorStatus == null || errorStatus.Errors != null && errorStatus.Errors.Count > 0)
return null; //just show individual field errors
var divTag = new TagBuilder("div");
HtmlHelper.ValidationSummaryCssClassNames.Split(' ').Each(divTag.AddCssClass);
divTag.MergeAttributes(htmlAttributes);
divTag.InnerHtml = errorStatus.Message;
return divTag.ToMvcHtmlString(TagRenderMode.Normal);
}
public static MvcHtmlString ValidationSuccess(this HtmlHelper htmlHelper, string message, object htmlAttributes=null)
{
return ValidationSuccess(htmlHelper, message, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString ValidationSuccess(this HtmlHelper htmlHelper, string message, IDictionary<string, object> htmlAttributes)
{
if (htmlHelper == null)
throw new ArgumentNullException("htmlHelper");
var errorStatus = htmlHelper.GetErrorStatus();
if (message == null
|| errorStatus != null
|| htmlHelper.HttpRequest.HttpMethod == HttpMethods.Get)
return null;
var divTag = new TagBuilder("div");
HtmlHelper.ValidationSuccessCssClassNames.Split(' ').Each(divTag.AddCssClass);
divTag.MergeAttributes(htmlAttributes);
divTag.InnerHtml = message;
return divTag.ToMvcHtmlString(TagRenderMode.Normal);
}
}
}
#endif