Changed to use Authenticate asp.net event instead of Authorize
[castle.git] / Components / Validator / Castle.Components.Validator / Attributes / AbstractValidationAttribute.cs
blobd52c91398cd3a59db771fb079620fc99d383ad14
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;
19 /// <summary>
20 /// Represents "phases" in which you can group
21 /// different validations and run then accordingly
22 /// </summary>
23 [Flags]
24 public enum RunWhen
26 /// <summary>
27 /// Run all validations
28 /// </summary>
29 Everytime = 0x1,
30 /// <summary>
31 /// Only during an insertion phase
32 /// </summary>
33 Insert = 0x2,
34 /// <summary>
35 /// Only during an update phase
36 /// </summary>
37 Update = 0x4,
38 /// <summary>
39 /// Defines a custom phase
40 /// </summary>
41 Custom = 0x8,
44 /// <summary>
45 /// The base class for all the validation attributes.
46 /// This class define a <seealso cref="Validator"/> property that is used to retrieve the validtor that is used to
47 /// validate the value of the property.
48 /// </summary>
49 [AttributeUsage(AttributeTargets.Property, AllowMultiple=true), Serializable]
50 public abstract class AbstractValidationAttribute : Attribute, IValidatorBuilder
52 private readonly string errorMessage;
53 private string friendlyName;
54 private int executionOrder = 0;
55 private RunWhen runWhen = RunWhen.Everytime;
57 /// <summary>
58 /// Initializes a new instance of the <see cref="AbstractValidationAttribute"/> class.
59 /// </summary>
60 protected AbstractValidationAttribute()
64 /// <summary>
65 /// Initializes a new instance of the <see cref="AbstractValidationAttribute"/> class.
66 /// </summary>
67 /// <param name="errorMessage">The error message.</param>
68 protected AbstractValidationAttribute(string errorMessage)
70 this.errorMessage = errorMessage;
73 /// <summary>
74 /// Defines when to run the validation.
75 /// Defaults to <c>RunWhen.Everytime</c>
76 /// </summary>
77 public RunWhen RunWhen
79 get { return runWhen; }
80 set { runWhen = value; }
83 /// <summary>
84 /// Gets or sets the validation execution order.
85 /// </summary>
86 /// <value>The execution order.</value>
87 public int ExecutionOrder
89 get { return executionOrder; }
90 set { executionOrder = value; }
93 /// <summary>
94 /// Gets or sets the a friendly name for the target property
95 /// </summary>
96 /// <value>The name.</value>
97 public string FriendlyName
99 get { return friendlyName; }
100 set { friendlyName = value; }
103 /// <summary>
104 /// Gets the error message.
105 /// </summary>
106 /// <value>The error message.</value>
107 protected string ErrorMessage
109 get { return errorMessage; }
112 /// <summary>
113 /// Constructs and configures an <see cref="IValidator"/>
114 /// instance based on the properties set on the attribute instance.
115 /// </summary>
116 /// <returns></returns>
117 public abstract IValidator Build();
120 /// <summary>
121 /// Constructs and configures an <see cref="IValidator"/>
122 /// instance based on the properties set on the attribute instance.
123 /// </summary>
124 public virtual IValidator Build(ValidatorRunner validatorRunner, Type type)
126 return Build();
129 /// <summary>
130 /// Applies the common configuration defined on the attribute.
131 /// </summary>
132 /// <param name="validator">The validator instance.</param>
133 protected void ConfigureValidatorMessage(IValidator validator)
135 validator.RunWhen = runWhen;
136 validator.ExecutionOrder = executionOrder;
138 if (errorMessage != null)
140 validator.ErrorMessage = errorMessage;
142 if (friendlyName != null)
144 validator.FriendlyName = friendlyName;