Fixing an issue where setting a custom property on a handler will not propagate it...
[castle.git] / Components / General / Validator / Castle.Components.Validator / Validators / SameAsValidator.cs
blobc42305c13caa41da2a3740b842ba08af4a3a9676
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.Collections;
19 /// <summary>
20 /// Validates that the content has the same
21 /// value as the property informed.
22 /// </summary>
23 public class SameAsValidator : AbstractValidator
25 private readonly string propertyToCompare;
27 /// <summary>
28 /// Initializes a new instance of the <see cref="SameAsValidator"/> class.
29 /// </summary>
30 /// <param name="propertyToCompare">The property to compare.</param>
31 public SameAsValidator(string propertyToCompare)
33 this.propertyToCompare = propertyToCompare;
37 /// <summary>
38 /// Gets the property to compare.
39 /// </summary>
40 /// <value>The property to compare.</value>
41 public string PropertyToCompare
43 get { return propertyToCompare; }
46 /// <summary>
47 /// Validates that the <c>fieldValue</c>
48 /// is the same as the property set through the constructor.
49 /// </summary>
50 /// <param name="instance">The target type instance</param>
51 /// <param name="fieldValue">The property/field value. It can be null.</param>
52 /// <returns>
53 /// <c>true</c> if the value is accepted (has passed the validation test)
54 /// </returns>
55 public override bool IsValid(object instance, object fieldValue)
57 object referenceValue = GetFieldOrPropertyValue(instance, propertyToCompare);
59 if (fieldValue is string && string.IsNullOrEmpty((string) fieldValue))
61 fieldValue = null;
63 if (fieldValue is string && string.IsNullOrEmpty((string) referenceValue))
65 referenceValue = null;
68 if (fieldValue == null && referenceValue == null)
70 return true;
72 else if (fieldValue != null)
74 return fieldValue.Equals(referenceValue);
76 else
78 return referenceValue.Equals(fieldValue);
82 /// <summary>
83 /// Gets a value indicating whether this validator supports browser validation.
84 /// </summary>
85 /// <value>
86 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
87 /// </value>
88 public override bool SupportsBrowserValidation
90 get { return true; }
93 /// <summary>
94 /// Applies the browser validation by setting up one or
95 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
96 /// </summary>
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,
104 string target)
106 base.ApplyBrowserValidation(config, inputType, generator, attributes, target);
108 generator.SetAsSameAs(target, propertyToCompare, BuildErrorMessage());
111 /// <summary>
112 /// Returns the key used to internationalize error messages
113 /// </summary>
114 /// <value></value>
115 protected override string MessageKey
117 get { return MessageConstants.SameAsMessage; }