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
20 /// Represents "phases" in which you can group
21 /// different validations and run then accordingly
27 /// Run all validations
31 /// Only during an insertion phase
35 /// Only during an update phase
39 /// Defines a custom phase
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.
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
;
58 /// Initializes a new instance of the <see cref="AbstractValidationAttribute"/> class.
60 protected AbstractValidationAttribute()
65 /// Initializes a new instance of the <see cref="AbstractValidationAttribute"/> class.
67 /// <param name="errorMessage">The error message.</param>
68 protected AbstractValidationAttribute(string errorMessage
)
70 this.errorMessage
= errorMessage
;
74 /// Defines when to run the validation.
75 /// Defaults to <c>RunWhen.Everytime</c>
77 public RunWhen RunWhen
79 get { return runWhen; }
80 set { runWhen = value; }
84 /// Gets or sets the validation execution order.
86 /// <value>The execution order.</value>
87 public int ExecutionOrder
89 get { return executionOrder; }
90 set { executionOrder = value; }
94 /// Gets or sets the a friendly name for the target property
96 /// <value>The name.</value>
97 public string FriendlyName
99 get { return friendlyName; }
100 set { friendlyName = value; }
104 /// Gets the error message.
106 /// <value>The error message.</value>
107 protected string ErrorMessage
109 get { return errorMessage; }
113 /// Constructs and configures an <see cref="IValidator"/>
114 /// instance based on the properties set on the attribute instance.
116 /// <returns></returns>
117 public abstract IValidator
Build();
121 /// Constructs and configures an <see cref="IValidator"/>
122 /// instance based on the properties set on the attribute instance.
124 public virtual IValidator
Build(ValidatorRunner validatorRunner
, Type type
)
130 /// Applies the common configuration defined on the attribute.
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
;