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
19 using Castle
.Components
.Validator
;
22 /// Validate that the field has a value in a set of values.
24 [Serializable
, CLSCompliant(false)]
25 public class ValidateSetAttribute
: AbstractValidationAttribute
27 private readonly IValidator validator
;
30 /// Initializes a new instance of the <see cref="ValidateSetAttribute"/> class.
32 public ValidateSetAttribute() : base()
34 validator
= new SetValidator();
38 /// Initializes a new instance of the <see cref="ValidateSetAttribute"/> class.
40 /// <param name="errorMessage">The error message to be displayed if the validation fails.</param>
41 public ValidateSetAttribute(string errorMessage
) : base(errorMessage
)
43 validator
= new SetValidator();
47 /// Initializes a new instance of the <see cref="ValidateSetAttribute"/> class.
49 /// <param name="set">The set of values to compare against.</param>
50 public ValidateSetAttribute(params string[] set) : base()
52 validator
= new SetValidator(set);
56 /// Initializes a new instance of the <see cref="ValidateSetAttribute"/> class.
58 /// <param name="errorMessage">The error message to be displayed if the validation fails.</param>
59 /// <param name="set">The set of values to compare against.</param>
60 public ValidateSetAttribute(string errorMessage
, params string[] set) : base(errorMessage
)
62 validator
= new SetValidator(set);
66 /// Initializes a new instance of the <see cref="ValidateSetAttribute"/> class.
68 /// <param name="type">The <see cref="System.Type" /> of an <c>enum</c> class.
69 /// The enum names will be added to the contents of the set.</param>
70 public ValidateSetAttribute(Type type
) : base()
72 validator
= new SetValidator(type
);
76 /// Initializes a new instance of the <see cref="ValidateSetAttribute"/> class.
78 /// <param name="type">The <see cref="System.Type" /> of an <c>enum</c> class.
79 /// The enum names will be added to the contents of the set.</param>
80 /// <param name="errorMessage">The error message to be displayed if the validation fails.</param>
81 public ValidateSetAttribute(Type type
, string errorMessage
) : base(errorMessage
)
83 validator
= new SetValidator(type
);
87 /// Constructs and configures an <see cref="IValidator"/>
88 /// instance based on the properties set on the attribute instance.
90 /// <returns></returns>
91 public override IValidator
Build()
93 ConfigureValidatorMessage(validator
);