1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
19 using Castle
.Components
.Validator
;
22 /// Implementation of a browser validator that uses the <c>fValidate</c>
23 /// javascript library.
25 public class FValidateWebValidator
: IBrowserValidatorProvider
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"/>
32 /// <param name="parameters"></param>
34 /// An instance that extends from <see cref="BrowserValidationConfiguration"/>
36 public BrowserValidationConfiguration
CreateConfiguration(IDictionary parameters
)
38 FValidateConfiguration config
= new FValidateConfiguration();
39 config
.Configure(parameters
);
44 /// Implementors should return their generator instance.
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
);
58 /// Supported configuration for fValidate.
60 public class FValidateConfiguration
: BrowserValidationConfiguration
63 /// Configures the JS library based on the supplied parameters.
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;
109 /// Generator for fValidate validation.
111 public class FValidateGenerator
: IBrowserValidationGenerator
113 private readonly InputElementType inputType
;
114 private readonly IDictionary attributes
;
117 /// Initializes a new instance of the <see cref="FValidateGenerator"/> class.
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
;
128 /// Sets that a field is required.
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
);
139 /// Sets that a field value must match the specified regular expression.
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();
150 /// Sets that a field value must be a valid email address.
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
);
161 /// Set that a field should only accept digits.
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
)
170 /// Set that a field should only accept numbers.
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
)
179 /// Sets that field must have an exact lenght.
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
)
189 /// Sets that field must have an exact lenght.
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
)
200 /// Sets that field must have an minimum lenght.
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);
210 /// Sets that field must have an minimum lenght.
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
);
221 /// Sets that field must have an maximum lenght.
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);
231 /// Sets that field must have an maximum lenght.
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
)
242 /// Sets that field must be between a length range.
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
);
253 /// Sets that field must be between a length range.
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
);
265 /// Sets that field must be between a value range.
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
)
277 /// Sets that field must be between a value range.
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
)
289 /// Sets that field must be between a value range.
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
)
301 /// Sets that field must be between a value range.
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
)
313 /// Set that a field value must be the same as another field's value.
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
)
323 /// Set that a field value must _not_ be the same as another field's value.
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
)
333 /// Set that a field value must be a valid date.
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
)
342 /// Adds the validator.
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
;
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
;
370 attributes
["emsg"] = violationMessage
;