Added RedirectUsingNamedRoute
[castle.git] / Components / Validator / Castle.Components.Validator / Validators / GroupNotEmptyValidator.cs
blobb0b2174e6ac07f0d396cd10841616435670a56b3
1 // Copyright 2004-2008 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.
16 namespace Castle.Components.Validator
18 using System;
19 using System.Collections;
20 using System.Reflection;
21 using System.Text;
23 /// <summary>
24 /// Ensures that at least one property in the group was filled with some value
25 /// </summary>
26 [Serializable]
27 public class GroupNotEmptyValidator : IValidator
29 private IDictionary properties = new Hashtable();
30 private RunWhen runWhen = RunWhen.Everytime;
31 private int executionOrder;
32 private string errorMessage;
33 private string friendlyName;
34 private string groupName;
35 private IValidatorRegistry validationRegistry;
37 /// <summary>
38 /// Initializes a new instance of the <see cref="GroupNotEmptyValidator"/> class.
39 /// </summary>
40 /// <param name="groupName">Name of the group.</param>
41 public GroupNotEmptyValidator(string groupName)
43 this.groupName = groupName;
46 /// <summary>
47 /// Implementors should perform any initialization logic
48 /// </summary>
49 /// <param name="validationRegistry"></param>
50 /// <param name="property">The target property</param>
51 public void Initialize(IValidatorRegistry validationRegistry, PropertyInfo property)
53 this.validationRegistry = validationRegistry;
54 properties[property] = FriendlyName ?? property.Name;
57 /// <summary>
58 /// The target property
59 /// </summary>
60 public PropertyInfo Property
62 get { throw new NotSupportedException("Group validator has more than a single property"); }
65 /// <summary>
66 /// Defines when to run the validation.
67 /// Defaults to <c>RunWhen.Everytime</c>
68 /// </summary>
69 public RunWhen RunWhen
71 get { return runWhen; }
72 set { runWhen = value; }
75 /// <summary>
76 /// Gets or sets the validation execution order.
77 /// </summary>
78 /// <value>The execution order.</value>
79 public int ExecutionOrder
81 get { return executionOrder; }
82 set { executionOrder = value; }
85 /// <summary>
86 /// The error message to be displayed if the validation fails
87 /// </summary>
88 /// <value>The error message.</value>
89 public string ErrorMessage
91 get
93 if (errorMessage == null)
95 errorMessage = BuildErrorMessage();
98 return errorMessage;
100 set { errorMessage = value; }
103 private string BuildErrorMessage()
105 StringBuilder sb = new StringBuilder();
107 string seperator =
108 validationRegistry.GetStringFromResource(MessageConstants.GroupNotEmptySeperator);
110 foreach(string name in properties.Values)
112 sb.Append(name).Append(seperator);
115 if (sb.Length > 0)
117 sb.Remove(sb.Length - seperator.Length, seperator.Length);
119 string messageFormat = validationRegistry.GetStringFromResource(MessageConstants.GroupNotEmpty);
121 return string.Format(messageFormat, sb);
124 /// <summary>
125 /// Gets or sets the a friendly name for the target property
126 /// </summary>
127 /// <value>The name.</value>
128 public string FriendlyName
130 get { return friendlyName; }
131 set { friendlyName = value; }
134 /// <summary>
135 /// Implementors should perform the actual validation upon
136 /// the property value
137 /// </summary>
138 /// <param name="instance"></param>
139 /// <returns><c>true</c> if the field is OK</returns>
140 public bool IsValid(object instance)
142 bool result = false;
144 foreach(PropertyInfo info in properties.Keys)
146 object o = info.GetValue(instance, null);
148 result |= o != null && o.ToString().Length != 0;
151 return result;
154 /// <summary>
155 /// Implementors should perform the actual validation upon
156 /// the property value
157 /// </summary>
158 /// <param name="instance"></param>
159 /// <param name="fieldValue"></param>
160 /// <returns><c>true</c> if the field is OK</returns>
161 public bool IsValid(object instance, object fieldValue)
163 throw new NotSupportedException("Must validate on the entire instance, not a single property");
166 /// <summary>
167 /// Gets a value indicating whether this validator supports browser validation.
168 /// </summary>
169 /// <value>
170 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
171 /// </value>
172 public bool SupportsBrowserValidation
174 get { return false; }
177 /// <summary>
178 /// Applies the browser validation by setting up one or
179 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
180 /// </summary>
181 /// <param name="config">The config.</param>
182 /// <param name="inputType">Type of the input.</param>
183 /// <param name="generator">The generator.</param>
184 /// <param name="attributes">The attributes.</param>
185 /// <param name="name">The name.</param>
186 public void ApplyBrowserValidation(BrowserValidationConfiguration config, InputElementType inputType,
187 IBrowserValidationGenerator generator, IDictionary attributes,
188 string name)
192 /// <summary>
193 /// Gets the property name. The <see cref="FriendlyName"/>
194 /// is returned if non-null, otherwise it will return the property name.
195 /// </summary>
196 public string Name
198 get { return groupName; }