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
18 using System
.Collections
;
21 /// Ensures that a property's string representation
22 /// is within a given set of values.
25 public class SetValidator
: AbstractValidator
27 private string[] setdata
;
30 /// Initializes a set-based validator with an empty set.
38 /// Initializes a set-based validator.
40 /// <param name="set">The set of values to validate against.</param>
41 public SetValidator(params string[] set)
47 /// Initializes a set-based validator.
49 /// <param name="type">The <see cref="System.Type" /> of an <c>enum</c> class.
50 /// The enum names will be added to the contents of the set.</param>
51 public SetValidator(Type type
)
54 throw new ArgumentNullException("The 'type' parameter can not be 'null'.");
56 throw new ArgumentException("The 'type' parameter must be of type System.Enum");
58 setdata
= Enum
.GetNames(type
);
62 /// Gets or sets the set of values to validate against.
66 get { return setdata; }
67 set { setdata = value; }
71 /// Validate that the property value matches the set requirements.
73 /// <param name="instance">The target type instance</param>
74 /// <param name="fieldValue">The property/field value. It can be null.</param>
75 /// <returns><c>true</c> if the value is accepted (has passed the validation test)</returns>
76 public override bool IsValid(object instance
, object fieldValue
)
78 if ((fieldValue
== null) || (String
.IsNullOrEmpty(fieldValue
.ToString())))
81 // no value can pass if the set is null
88 for(int i
= 0; i
< setdata
.Length
; i
++)
90 if (String
.Equals(fieldValue
, setdata
[i
]))
107 /// Gets a value indicating whether this validator supports browser validation.
110 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
112 public override bool SupportsBrowserValidation
114 get { return false; }
118 /// Applies the browser validation by setting up one or
119 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
121 /// <param name="config">The config.</param>
122 /// <param name="inputType">Type of the input.</param>
123 /// <param name="generator">The generator.</param>
124 /// <param name="attributes">The attributes.</param>
125 /// <param name="target">The target.</param>
126 public override void ApplyBrowserValidation(BrowserValidationConfiguration config
, InputElementType inputType
,
127 IBrowserValidationGenerator generator
, IDictionary attributes
,
133 /// Builds the error message.
135 /// <returns></returns>
136 protected override string BuildErrorMessage()
138 return string.Format(GetString(MessageConstants
.InvalidSetMessage
));
142 /// Returns the key used to internationalize error messages
144 /// <returns></returns>
145 protected override string MessageKey
147 get { return MessageConstants.InvalidSetMessage; }