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
.Components
.Validator
20 /// Validate that this property has the required length (either exact or in a range)
22 [Serializable
, CLSCompliant(false)]
23 public class ValidateLengthAttribute
: AbstractValidationAttribute
25 private readonly IValidator validator
;
28 /// Initializes a new exact length validator.
30 /// <param name="exactLength">The exact length required.</param>
31 public ValidateLengthAttribute(int exactLength
)
33 validator
= new LengthValidator(exactLength
);
37 /// Initializes a new exact length validator.
39 /// <param name="exactLength">The exact length required.</param>
40 /// <param name="errorMessage">The error message to be displayed if the validation fails.</param>
41 public ValidateLengthAttribute(int exactLength
, String errorMessage
) : base(errorMessage
)
43 validator
= new LengthValidator(exactLength
);
47 /// Initializes a new range based length validator.
49 /// <param name="minLength">The minimum length, or <c>int.MinValue</c> if this should not be tested.</param>
50 /// <param name="maxLength">The maximum length, or <c>int.MaxValue</c> if this should not be tested.</param>
51 public ValidateLengthAttribute(int minLength
, int maxLength
)
53 validator
= new LengthValidator(minLength
, maxLength
);
57 /// Initializes a new range based length validator.
59 /// <param name="minLength">The minimum length, or <c>int.MinValue</c> if this should not be tested.</param>
60 /// <param name="maxLength">The maximum length, or <c>int.MaxValue</c> if this should not be tested.</param>
61 /// <param name="errorMessage">The error message to be displayed if the validation fails.</param>
62 public ValidateLengthAttribute(int minLength
, int maxLength
, String errorMessage
) : base(errorMessage
)
64 validator
= new LengthValidator(minLength
, maxLength
);
68 /// Constructs and configures an <see cref="IValidator"/>
69 /// instance based on the properties set on the attribute instance.
71 /// <returns></returns>
72 public override IValidator
Build()
74 ConfigureValidatorMessage(validator
);