- Fixed MR-84
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / ValidationStrategy / FValidateValidator.cs
blobdebb5f92d5ed952059ce0619b6128a1123c0e0cd
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Helpers.ValidationStrategy
17 using System;
18 using System.Collections;
19 using Castle.Components.Validator;
21 /// <summary>
22 /// Implementation of a browser validator that uses the <c>fValidate</c>
23 /// javascript library.
24 /// </summary>
25 public class FValidateWebValidator : IBrowserValidatorProvider
27 /// <summary>
28 /// Implementors should attempt to read their specific configuration
29 /// from the <paramref name="parameters"/>, configure and return
30 /// a class that extends from <see cref="BrowserValidationConfiguration"/>
31 /// </summary>
32 /// <param name="parameters"></param>
33 /// <returns>
34 /// An instance that extends from <see cref="BrowserValidationConfiguration"/>
35 /// </returns>
36 public BrowserValidationConfiguration CreateConfiguration(IDictionary parameters)
38 FValidateConfiguration config = new FValidateConfiguration();
39 config.Configure(parameters);
40 return config;
43 /// <summary>
44 /// Implementors should return their generator instance.
45 /// </summary>
46 /// <param name="config"></param>
47 /// <param name="inputType"></param>
48 /// <param name="attributes"></param>
49 /// <returns>A generator instance</returns>
50 public IBrowserValidationGenerator CreateGenerator(BrowserValidationConfiguration config, InputElementType inputType, IDictionary attributes)
52 return new FValidateGenerator(inputType, attributes);
55 #region Configuration
57 /// <summary>
58 /// Supported configuration for fValidate.
59 /// </summary>
60 public class FValidateConfiguration : BrowserValidationConfiguration
62 /// <summary>
63 /// Configures the JS library based on the supplied parameters.
64 /// </summary>
65 /// <param name="parameters">The parameters.</param>
66 public override void Configure(IDictionary parameters)
68 // ( f, bConfirm, bDisable, bDisableR, groupError, errorMode )
69 parameters["onsubmit"] = "return validateForm( this, 0, 1, 0, 1, 16 );";
71 case 0 : alertError(); break;
72 case 1 : inputError(); break;
73 case 2 : labelError(); break;
74 case 3 : appendError(); break;
75 case 4 : boxError(); break;
76 case 5 : inputError(); labelError(); break;
77 case 6 : inputError(); appendError(); break;
78 case 7 : inputError(); boxError(); break;
79 case 8 : inputError(); alertError(); break;
80 case 9 : labelError(); appendError(); break;
81 case 10 : labelError(); boxError(); break;
82 case 11 : labelError(); alertError(); break;
83 case 12 : appendError(); boxError(); break;
84 case 13 : appendError(); alertError(); break;
85 case 14 : boxError(); alertError(); break;
86 case 15 : inputError(); labelError(); appendError(); break;
87 case 16 : inputError(); labelError(); boxError(); break;
88 case 17 : inputError(); labelError(); alertError(); break;
89 case 18 : inputError(); appendError(); boxError(); break;
90 case 19 : inputError(); appendError(); alertError(); break;
91 case 20 : inputError(); boxError(); alertError(); break;
92 case 21 : labelError(); appendError(); boxError(); break;
93 case 22 : labelError(); appendError(); alertError(); break;
94 case 23 : appendError(); boxError(); alertError(); break;
95 case 24 : inputError(); labelError(); appendError(); boxError(); break;
96 case 25 : inputError(); labelError(); appendError(); alertError(); break;
97 case 26 : inputError(); appendError(); boxError(); alertError(); break;
98 case 27 : labelError(); appendError(); boxError(); alertError(); break;
99 case 28 : inputError(); labelError(); appendError(); boxError(); alertError(); break;
104 #endregion
106 #region Generator
108 /// <summary>
109 /// Generator for fValidate validation.
110 /// </summary>
111 public class FValidateGenerator : IBrowserValidationGenerator
113 private readonly InputElementType inputType;
114 private readonly IDictionary attributes;
116 /// <summary>
117 /// Initializes a new instance of the <see cref="FValidateGenerator"/> class.
118 /// </summary>
119 /// <param name="inputType">Type of the input.</param>
120 /// <param name="attributes">The attributes.</param>
121 public FValidateGenerator(InputElementType inputType, IDictionary attributes)
123 this.inputType = inputType;
124 this.attributes = attributes;
127 /// <summary>
128 /// Sets that a field is required.
129 /// </summary>
130 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
131 /// <param name="violationMessage">The violation message.</param>
132 public void SetAsRequired(string target, string violationMessage)
134 AddValidator(target, "blank");
135 AddErrorMessage(violationMessage);
138 /// <summary>
139 /// Sets that a field value must match the specified regular expression.
140 /// </summary>
141 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
142 /// <param name="regExp">The reg exp.</param>
143 /// <param name="violationMessage">The violation message.</param>
144 public void SetRegExp(string target, string regExp, string violationMessage)
146 throw new NotImplementedException();
149 /// <summary>
150 /// Sets that a field value must be a valid email address.
151 /// </summary>
152 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
153 /// <param name="violationMessage">The violation message.</param>
154 public void SetEmail(string target, string violationMessage)
156 AddValidator(target, "email|1");
157 AddErrorMessage(violationMessage);
160 /// <summary>
161 /// Set that a field should only accept digits.
162 /// </summary>
163 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
164 /// <param name="violationMessage">The violation message.</param>
165 public void SetDigitsOnly(string target, string violationMessage)
169 /// <summary>
170 /// Set that a field should only accept numbers.
171 /// </summary>
172 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
173 /// <param name="violationMessage">The violation message.</param>
174 public void SetNumberOnly(string target, string violationMessage)
178 /// <summary>
179 /// Sets that field must have an exact lenght.
180 /// </summary>
181 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
182 /// <param name="length">The length.</param>
183 public void SetExactLength(string target, int length)
185 // Not supported
188 /// <summary>
189 /// Sets that field must have an exact lenght.
190 /// </summary>
191 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
192 /// <param name="length">The length.</param>
193 /// <param name="violationMessage">The violation message.</param>
194 public void SetExactLength(string target, int length, string violationMessage)
196 // Not supported
199 /// <summary>
200 /// Sets that field must have an minimum lenght.
201 /// </summary>
202 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
203 /// <param name="minLength">The minimum length.</param>
204 public void SetMinLength(string target, int minLength)
206 SetMinLength(target, minLength, null);
209 /// <summary>
210 /// Sets that field must have an minimum lenght.
211 /// </summary>
212 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
213 /// <param name="minLength">The minimum length.</param>
214 /// <param name="violationMessage">The violation message.</param>
215 public void SetMinLength(string target, int minLength, string violationMessage)
217 AddValidator(target, "length|" + minLength);
220 /// <summary>
221 /// Sets that field must have an maximum lenght.
222 /// </summary>
223 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
224 /// <param name="maxLength">The maximum length.</param>
225 public void SetMaxLength(string target, int maxLength)
227 SetMaxLength(target, maxLength, null);
230 /// <summary>
231 /// Sets that field must have an maximum lenght.
232 /// </summary>
233 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
234 /// <param name="maxLength">The maximum length.</param>
235 /// <param name="violationMessage">The violation message.</param>
236 public void SetMaxLength(string target, int maxLength, string violationMessage)
238 // Not supported
241 /// <summary>
242 /// Sets that field must be between a length range.
243 /// </summary>
244 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
245 /// <param name="minLength">The minimum length.</param>
246 /// <param name="maxLength">The maximum length.</param>
247 public void SetLengthRange(string target, int minLength, int maxLength)
249 AddValidator(target, "length|" + minLength + "|" + maxLength);
252 /// <summary>
253 /// Sets that field must be between a length range.
254 /// </summary>
255 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
256 /// <param name="minLength">The minimum length.</param>
257 /// <param name="maxLength">The maximum length.</param>
258 /// <param name="violationMessage">The violation message.</param>
259 public void SetLengthRange(string target, int minLength, int maxLength, string violationMessage)
261 AddValidator(target, "length|" + minLength + "|" + maxLength);
264 /// <summary>
265 /// Sets that field must be between a value range.
266 /// </summary>
267 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
268 /// <param name="minValue">Minimum value.</param>
269 /// <param name="maxValue">Maximum value.</param>
270 /// <param name="violationMessage">The violation message.</param>
271 public void SetValueRange(string target, int minValue, int maxValue, string violationMessage)
276 /// <summary>
277 /// Sets that field must be between a value range.
278 /// </summary>
279 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
280 /// <param name="minValue">Minimum value.</param>
281 /// <param name="maxValue">Maximum value.</param>
282 /// <param name="violationMessage">The violation message.</param>
283 public void SetValueRange(string target, decimal minValue, decimal maxValue, string violationMessage)
288 /// <summary>
289 /// Sets that field must be between a value range.
290 /// </summary>
291 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
292 /// <param name="minValue">Minimum value.</param>
293 /// <param name="maxValue">Maximum value.</param>
294 /// <param name="violationMessage">The violation message.</param>
295 public void SetValueRange(string target, DateTime minValue, DateTime maxValue, string violationMessage)
300 /// <summary>
301 /// Sets that field must be between a value range.
302 /// </summary>
303 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
304 /// <param name="minValue">Minimum value.</param>
305 /// <param name="maxValue">Maximum value.</param>
306 /// <param name="violationMessage">The violation message.</param>
307 public void SetValueRange(string target, string minValue, string maxValue, string violationMessage)
312 /// <summary>
313 /// Set that a field value must be the same as another field's value.
314 /// </summary>
315 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
316 /// <param name="comparisonFieldName">The name of the field to compare with.</param>
317 /// <param name="violationMessage">The violation message.</param>
318 public void SetAsSameAs(string target, string comparisonFieldName, string violationMessage)
322 /// <summary>
323 /// Set that a field value must _not_ be the same as another field's value.
324 /// </summary>
325 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
326 /// <param name="comparisonFieldName">The name of the field to compare with.</param>
327 /// <param name="violationMessage">The violation message.</param>
328 public void SetAsNotSameAs(string target, string comparisonFieldName, string violationMessage)
332 /// <summary>
333 /// Set that a field value must be a valid date.
334 /// </summary>
335 /// <param name="target">The target name (ie, a hint about the controller being validated)</param>
336 /// <param name="violationMessage">The violation message.</param>
337 public void SetDate(string target, string violationMessage)
341 /// <summary>
342 /// Adds the validator.
343 /// </summary>
344 /// <param name="target">The target.</param>
345 /// <param name="validator">The validator.</param>
346 private void AddValidator(string target, string validator)
348 string existingValidators = (string) attributes["validators"];
350 if (existingValidators != null)
352 attributes["validators"] = existingValidators + "|" + validator;
354 else
356 attributes["validators"] = validator;
360 private void AddErrorMessage(string violationMessage)
362 string existingMessage = (string) attributes["emsg"];
364 if (existingMessage != null)
366 attributes["emsg"] = existingMessage + "," + violationMessage;
368 else
370 attributes["emsg"] = violationMessage;
375 #endregion