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
17 using System
.Collections
;
18 using System
.Reflection
;
21 /// Defines the basic contract for validators.
23 /// To create a new validation you should use <see cref="AbstractValidator"/> as it
24 /// implements most of the common methods and properties.
27 /// The validation should happen at <c>IsValid</c>, and if the validator can configure
28 /// a client-side validation script, it should use the <see cref="SupportsBrowserValidation"/>
29 /// to indicate that it does support client-side validation and also implement the
30 /// <see cref="ApplyBrowserValidation"/> to configure it.
33 public interface IValidator
36 /// Implementors should perform any initialization logic
38 /// <param name="validationRegistry">The validation registry.</param>
39 /// <param name="property">The target property</param>
40 void Initialize(IValidatorRegistry validationRegistry
, PropertyInfo property
);
43 /// The target property
45 PropertyInfo Property { get; }
48 /// Defines when to run the validation.
49 /// Defaults to <c>RunWhen.Everytime</c>
51 RunWhen RunWhen { get; set; }
54 /// Gets or sets the validation execution order.
56 /// <value>The execution order.</value>
57 int ExecutionOrder { get; set; }
60 /// The error message to be displayed if the validation fails
62 /// <value>The error message.</value>
63 string ErrorMessage { get; set; }
66 /// Gets or sets the a friendly name for the target property
68 /// <value>The name.</value>
69 string FriendlyName { get; set; }
72 /// Implementors should perform the actual validation upon
73 /// the property value
75 /// <param name="instance"></param>
76 /// <returns><c>true</c> if the field is OK</returns>
77 bool IsValid(object instance
);
80 /// Implementors should perform the actual validation upon
81 /// the property value
83 /// <param name="instance"></param>
84 /// <param name="fieldValue"></param>
85 /// <returns><c>true</c> if the field is OK</returns>
86 bool IsValid(object instance
, object fieldValue
);
89 /// Gets a value indicating whether this validator supports browser validation.
92 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
94 bool SupportsBrowserValidation { get; }
97 /// Applies the browser validation by setting up one or
98 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
100 /// <param name="config">The config.</param>
101 /// <param name="inputType">Type of the input.</param>
102 /// <param name="generator">The generator.</param>
103 /// <param name="attributes">The attributes.</param>
104 /// <param name="target">The target.</param>
105 void ApplyBrowserValidation(BrowserValidationConfiguration config
, InputElementType inputType
,
106 IBrowserValidationGenerator generator
, IDictionary attributes
, string target
);
109 /// Gets the property name. The <see cref="FriendlyName"/>
110 /// is returned if non-null, otherwise it will return the property name.