Fixing an issue where setting a custom property on a handler will not propagate it...
[castle.git] / Components / General / Validator / Castle.Components.Validator / Validators / SetValidator.cs
blob4e243a3f9680dc7628e750872dd4ec7b0a5668d5
1 // Copyright 2004-2007 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 a given set of values.
23 /// </summary>
24 [Serializable]
25 public class SetValidator : AbstractValidator
27 private string[] setdata;
29 /// <summary>
30 /// Initializes a set-based validator with an empty set.
31 /// </summary>
32 public SetValidator()
34 setdata = null;
37 /// <summary>
38 /// Initializes a set-based validator.
39 /// </summary>
40 /// <param name="set">The set of values to validate against.</param>
41 public SetValidator(params string[] set)
43 setdata = set;
46 /// <summary>
47 /// Initializes a set-based validator.
48 /// </summary>
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)
53 if (type == null)
54 throw new ArgumentNullException("The 'type' parameter can not be 'null'.");
55 if (!type.IsEnum)
56 throw new ArgumentException("The 'type' parameter must be of type System.Enum");
58 setdata = Enum.GetNames(type);
61 /// <summary>
62 /// Gets or sets the set of values to validate against.
63 /// </summary>
64 public string[] Set
66 get { return setdata; }
67 set { setdata = value; }
70 /// <summary>
71 /// Validate that the property value matches the set requirements.
72 /// </summary>
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())))
79 return true;
81 // no value can pass if the set is null
82 if (setdata == null)
83 return false;
85 try
87 bool found = false;
88 for(int i = 0; i < setdata.Length; i++)
90 if (String.Equals(fieldValue, setdata[i]))
92 found = true;
93 break;
97 return found;
99 catch
103 return false;
106 /// <summary>
107 /// Gets a value indicating whether this validator supports browser validation.
108 /// </summary>
109 /// <value>
110 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
111 /// </value>
112 public override bool SupportsBrowserValidation
114 get { return false; }
117 /// <summary>
118 /// Applies the browser validation by setting up one or
119 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
120 /// </summary>
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,
128 string target)
132 /// <summary>
133 /// Builds the error message.
134 /// </summary>
135 /// <returns></returns>
136 protected override string BuildErrorMessage()
138 return string.Format(GetString(MessageConstants.InvalidSetMessage));
141 /// <summary>
142 /// Returns the key used to internationalize error messages
143 /// </summary>
144 /// <returns></returns>
145 protected override string MessageKey
147 get { return MessageConstants.InvalidSetMessage; }