1 // Copyright 2004-2008 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
.Components
.Validator
18 using System
.Collections
;
21 /// Ensures that a property's string representation
22 /// is within the desired length limitations.
25 public class LengthValidator
: AbstractValidator
27 private int exactLength
= int.MinValue
;
28 private int minLength
= int.MinValue
;
29 private int maxLength
= int.MaxValue
;
32 /// Initializes a new exact length validator.
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
;
46 /// Initializes a new range based length validator.
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.");
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
;
81 /// Gets or sets the exact length to validate.
83 /// <value>The exact length to validate.</value>
84 public int ExactLength
86 get { return exactLength; }
87 set { exactLength = value; }
91 /// Gets or sets the minimun length to validate.
93 /// <value>The minimun length to validate.</value>
96 get { return minLength; }
97 set { minLength = value; }
101 /// Gets or sets the maximum length to validate.
103 /// <value>The maximum length to validate.</value>
106 get { return maxLength; }
107 set { maxLength = value; }
111 /// Validate that the property value matches the length requirements.
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;
136 throw new InvalidOperationException();
141 /// Gets a value indicating whether this validator supports browser validation.
144 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
146 public override bool SupportsBrowserValidation
152 /// Applies the browser validation by setting up one or
153 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
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
,
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
);
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
);
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
);
195 /// Builds the error message.
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
)
215 string.Format(GetString(MessageConstants
.LengthInRangeMessage
), minLength
, maxLength
);
219 throw new InvalidOperationException();