Reformatting the code according to castle guidelines.
[castle.git] / Components / General / Validator / Castle.Components.Validator / Validators / IntegerValidator.cs
blobc51e4ec4ddc656531b1ecc6ed9b8dfb7648e2768
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;
18 using System.Collections;
20 /// <summary>
21 /// This is a meta validator.
22 /// It is only useful to test a source content before setting it on the
23 /// target instance.
24 /// </summary>
25 public class IntegerValidator : AbstractValidator
27 /// <summary>
28 /// If the <c>fieldValue</c> is not null, an attempt to convert the
29 /// content to a Integer is performed, and the field is considered value
30 /// if the conversion is successful.
31 /// </summary>
32 /// <param name="instance">The target type instance</param>
33 /// <param name="fieldValue">The property/field value. It can be null.</param>
34 /// <returns>
35 /// <c>true</c> if the value is accepted (has passed the validation test)
36 /// </returns>
37 public override bool IsValid(object instance, object fieldValue)
39 if (fieldValue == null) return false;
41 try
43 if (Property.PropertyType == typeof(Int16))
45 Convert.ToInt16(fieldValue.ToString());
47 else if (Property.PropertyType == typeof(Int64))
49 Convert.ToInt64(fieldValue.ToString());
51 else
53 Convert.ToInt32(fieldValue.ToString());
56 return true;
58 catch(Exception)
60 return false;
64 /// <summary>
65 /// Gets a value indicating whether this validator supports web validation.
66 /// </summary>
67 /// <value>
68 /// <see langword="true"/> if web validation is supported; otherwise, <see langword="false"/>.
69 /// </value>
70 public override bool SupportsWebValidation
72 get { return true; }
75 /// <summary>
76 /// Applies the web validation by setting up one or
77 /// more input rules on <see cref="IWebValidationGenerator"/>.
78 /// </summary>
79 /// <param name="config">The config.</param>
80 /// <param name="inputType">Type of the input.</param>
81 /// <param name="generator">The generator.</param>
82 /// <param name="attributes">The attributes.</param>
83 /// <param name="target">The target.</param>
84 public override void ApplyWebValidation(WebValidationConfiguration config, InputElementType inputType,
85 IWebValidationGenerator generator, IDictionary attributes, string target)
87 base.ApplyWebValidation(config, inputType, generator, attributes, target);
89 if (inputType == InputElementType.Text)
91 generator.SetDigitsOnly(BuildErrorMessage());
95 /// <summary>
96 /// Returns the key used to internationalize error messages
97 /// </summary>
98 /// <value></value>
99 protected override string MessageKey
101 get { return MessageConstants.InvalidIntegerMessage; }