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
;
20 /// Validates that the content has the same
21 /// value as the property informed.
23 public class SameAsValidator
: AbstractValidator
25 private readonly string propertyToCompare
;
28 /// Initializes a new instance of the <see cref="SameAsValidator"/> class.
30 /// <param name="propertyToCompare">The property to compare.</param>
31 public SameAsValidator(string propertyToCompare
)
33 this.propertyToCompare
= propertyToCompare
;
38 /// Gets the property to compare.
40 /// <value>The property to compare.</value>
41 public string PropertyToCompare
43 get { return propertyToCompare; }
47 /// Validates that the <c>fieldValue</c>
48 /// is the same as the property set through the constructor.
50 /// <param name="instance">The target type instance</param>
51 /// <param name="fieldValue">The property/field value. It can be null.</param>
53 /// <c>true</c> if the value is accepted (has passed the validation test)
55 public override bool IsValid(object instance
, object fieldValue
)
57 object referenceValue
= GetFieldOrPropertyValue(instance
, propertyToCompare
);
59 if (fieldValue
is string && string.IsNullOrEmpty((string) fieldValue
))
63 if (fieldValue
is string && string.IsNullOrEmpty((string) referenceValue
))
65 referenceValue
= null;
68 if (fieldValue
== null && referenceValue
== null)
72 else if (fieldValue
!= null)
74 return fieldValue
.Equals(referenceValue
);
78 return referenceValue
.Equals(fieldValue
);
83 /// Gets a value indicating whether this validator supports browser validation.
86 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
88 public override bool SupportsBrowserValidation
94 /// Applies the browser validation by setting up one or
95 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
97 /// <param name="config">The config.</param>
98 /// <param name="inputType">Type of the input.</param>
99 /// <param name="generator">The generator.</param>
100 /// <param name="attributes">The attributes.</param>
101 /// <param name="target">The target.</param>
102 public override void ApplyBrowserValidation(BrowserValidationConfiguration config
, InputElementType inputType
,
103 IBrowserValidationGenerator generator
, IDictionary attributes
,
106 base.ApplyBrowserValidation(config
, inputType
, generator
, attributes
, target
);
108 generator
.SetAsSameAs(target
, propertyToCompare
, BuildErrorMessage());
112 /// Returns the key used to internationalize error messages
115 protected override string MessageKey
117 get { return MessageConstants.SameAsMessage; }