1 // Copyright 2004-2008 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.
16 namespace Castle
.Components
.Validator
19 using System
.Collections
;
20 using System
.Reflection
;
24 /// Ensures that at least one property in the group was filled with some value
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
;
38 /// Initializes a new instance of the <see cref="GroupNotEmptyValidator"/> class.
40 /// <param name="groupName">Name of the group.</param>
41 public GroupNotEmptyValidator(string groupName
)
43 this.groupName
= groupName
;
47 /// Implementors should perform any initialization logic
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
;
58 /// The target property
60 public PropertyInfo Property
62 get { throw new NotSupportedException("Group validator has more than a single property"); }
66 /// Defines when to run the validation.
67 /// Defaults to <c>RunWhen.Everytime</c>
69 public RunWhen RunWhen
71 get { return runWhen; }
72 set { runWhen = value; }
76 /// Gets or sets the validation execution order.
78 /// <value>The execution order.</value>
79 public int ExecutionOrder
81 get { return executionOrder; }
82 set { executionOrder = value; }
86 /// The error message to be displayed if the validation fails
88 /// <value>The error message.</value>
89 public string ErrorMessage
93 if (errorMessage
== null)
95 errorMessage
= BuildErrorMessage();
100 set { errorMessage = value; }
103 private string BuildErrorMessage()
105 StringBuilder sb
= new StringBuilder();
108 validationRegistry
.GetStringFromResource(MessageConstants
.GroupNotEmptySeperator
);
110 foreach(string name
in properties
.Values
)
112 sb
.Append(name
).Append(seperator
);
117 sb
.Remove(sb
.Length
- seperator
.Length
, seperator
.Length
);
119 string messageFormat
= validationRegistry
.GetStringFromResource(MessageConstants
.GroupNotEmpty
);
121 return string.Format(messageFormat
, sb
);
125 /// Gets or sets the a friendly name for the target property
127 /// <value>The name.</value>
128 public string FriendlyName
130 get { return friendlyName; }
131 set { friendlyName = value; }
135 /// Implementors should perform the actual validation upon
136 /// the property value
138 /// <param name="instance"></param>
139 /// <returns><c>true</c> if the field is OK</returns>
140 public bool IsValid(object instance
)
144 foreach(PropertyInfo info
in properties
.Keys
)
146 object o
= info
.GetValue(instance
, null);
148 result
|= o
!= null && o
.ToString().Length
!= 0;
155 /// Implementors should perform the actual validation upon
156 /// the property value
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");
167 /// Gets a value indicating whether this validator supports browser validation.
170 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
172 public bool SupportsBrowserValidation
174 get { return false; }
178 /// Applies the browser validation by setting up one or
179 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
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
,
193 /// Gets the property name. The <see cref="FriendlyName"/>
194 /// is returned if non-null, otherwise it will return the property name.
198 get { return groupName; }