Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Components / Validator / Castle.Components.Validator / Validators / LengthValidator.cs
blob27598d1d12ba4c2f502d96ee67ad14a3e8f40f68
1 // Copyright 2004-2008 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.Components.Validator
17 using System;
18 using System.Collections;
20 /// <summary>
21 /// Ensures that a property's string representation
22 /// is within the desired length limitations.
23 /// </summary>
24 [Serializable]
25 public class LengthValidator : AbstractValidator
27 private int exactLength = int.MinValue;
28 private int minLength = int.MinValue;
29 private int maxLength = int.MaxValue;
31 /// <summary>
32 /// Initializes a new exact length validator.
33 /// </summary>
34 /// <param name="exactLength">The exact length required.</param>
35 public LengthValidator(int exactLength)
37 if (minLength != int.MinValue && minLength < 0)
39 throw new ArgumentOutOfRangeException("The exactLength parameter must be set to a non-negative number.");
42 this.exactLength = exactLength;
45 /// <summary>
46 /// Initializes a new range based length validator.
47 /// </summary>
48 /// <param name="minLength">The minimum length, or <c>int.MinValue</c> if this should not be tested.</param>
49 /// <param name="maxLength">The maximum length, or <c>int.MaxValue</c> if this should not be tested.</param>
50 public LengthValidator(int minLength, int maxLength)
52 if (minLength == int.MinValue && maxLength == int.MaxValue)
54 throw new ArgumentException(
55 "Both minLength and maxLength were set in such as way that neither would be tested. At least one must be tested.");
58 if (minLength > maxLength)
60 throw new ArgumentException("The maxLength parameter must be greater than the minLength parameter.");
63 if (minLength != int.MinValue && minLength < 0)
65 throw new ArgumentOutOfRangeException(
66 "The minLength parameter must be set to either int.MinValue or a non-negative number.");
69 if (maxLength < 0)
71 throw new ArgumentOutOfRangeException(
72 "The maxLength parameter must be set to either int.MaxValue or a non-negative number.");
75 this.minLength = minLength;
76 this.maxLength = maxLength;
80 /// <summary>
81 /// Gets or sets the exact length to validate.
82 /// </summary>
83 /// <value>The exact length to validate.</value>
84 public int ExactLength
86 get { return exactLength; }
87 set { exactLength = value; }
90 /// <summary>
91 /// Gets or sets the minimun length to validate.
92 /// </summary>
93 /// <value>The minimun length to validate.</value>
94 public int MinLength
96 get { return minLength; }
97 set { minLength = value; }
100 /// <summary>
101 /// Gets or sets the maximum length to validate.
102 /// </summary>
103 /// <value>The maximum length to validate.</value>
104 public int MaxLength
106 get { return maxLength; }
107 set { maxLength = value; }
110 /// <summary>
111 /// Validate that the property value matches the length requirements.
112 /// </summary>
113 /// <param name="instance"></param>
114 /// <param name="fieldValue"></param>
115 /// <returns><c>true</c> if the field is OK</returns>
116 public override bool IsValid(object instance, object fieldValue)
118 if (fieldValue == null) return true;
120 int length = fieldValue.ToString().Length;
121 if (length == 0) return true;
123 if (exactLength != int.MinValue)
125 return (length == exactLength);
127 else if (minLength != int.MinValue || maxLength != int.MaxValue)
129 if (minLength != int.MinValue && length < minLength) return false;
130 if (maxLength != int.MaxValue && length > maxLength) return false;
132 return true;
134 else
136 throw new InvalidOperationException();
140 /// <summary>
141 /// Gets a value indicating whether this validator supports browser validation.
142 /// </summary>
143 /// <value>
144 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
145 /// </value>
146 public override bool SupportsBrowserValidation
148 get { return true; }
151 /// <summary>
152 /// Applies the browser validation by setting up one or
153 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
154 /// </summary>
155 /// <param name="config">The config.</param>
156 /// <param name="inputType">Type of the input.</param>
157 /// <param name="generator">The generator.</param>
158 /// <param name="attributes">The attributes.</param>
159 /// <param name="target">The target.</param>
160 public override void ApplyBrowserValidation(BrowserValidationConfiguration config, InputElementType inputType,
161 IBrowserValidationGenerator generator, IDictionary attributes,
162 string target)
164 base.ApplyBrowserValidation(config, inputType, generator, attributes, target);
166 if (exactLength != int.MinValue)
168 string message = string.Format(GetString(MessageConstants.ExactLengthMessage), exactLength);
169 generator.SetExactLength(target, exactLength, ErrorMessage ?? message);
171 else
173 if (minLength != int.MinValue && maxLength != int.MaxValue)
175 string message = string.Format(GetString(MessageConstants.LengthInRangeMessage), minLength, maxLength);
176 generator.SetLengthRange(target, minLength, maxLength, ErrorMessage ?? message);
178 else
180 if (minLength != int.MinValue)
182 string message = string.Format(GetString(MessageConstants.LengthTooShortMessage), minLength);
183 generator.SetMinLength(target, minLength, ErrorMessage ?? message);
185 if (maxLength != int.MaxValue)
187 string message = string.Format(GetString(MessageConstants.LengthTooLongMessage), maxLength);
188 generator.SetMaxLength(target, maxLength, ErrorMessage ?? message);
194 /// <summary>
195 /// Builds the error message.
196 /// </summary>
197 /// <returns></returns>
198 protected override string BuildErrorMessage()
200 if (exactLength != int.MinValue)
202 return string.Format(GetString(MessageConstants.ExactLengthMessage), exactLength);
204 else if (minLength == int.MinValue && maxLength != int.MaxValue)
206 return string.Format(GetString(MessageConstants.LengthTooLongMessage), maxLength);
208 else if (minLength != int.MinValue && maxLength == int.MaxValue)
210 return string.Format(GetString(MessageConstants.LengthTooShortMessage), minLength);
212 else if (minLength != int.MinValue || maxLength != int.MaxValue)
214 return
215 string.Format(GetString(MessageConstants.LengthInRangeMessage), minLength, maxLength);
217 else
219 throw new InvalidOperationException();