Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Components / Validator / Castle.Components.Validator / Validators / IntegerValidator.cs
blob595be31c2aca876a14994c550ad2ab207d87b606
1 // Copyright 2004-2008 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 /// Checks if the <c>fieldValue</c> can be converted to a valid Integer.
29 /// Null or empty value are allowed.
30 /// </summary>
31 /// <param name="instance">The target type instance</param>
32 /// <param name="fieldValue">The property/field value. It can be null.</param>
33 /// <returns>
34 /// <c>true</c> if the value is accepted (has passed the validation test)
35 /// </returns>
36 public override bool IsValid(object instance, object fieldValue)
38 if (fieldValue == null || fieldValue.ToString() == "") return true;
40 string stringValue = fieldValue.ToString();
42 if (Property.PropertyType == typeof(Nullable<short>))
44 Int16 intValue;
45 return Int16.TryParse(stringValue, out intValue);
47 else if (Property.PropertyType == typeof(Nullable<Int64>))
49 Int64 intValue;
50 return Int64.TryParse(stringValue, out intValue);
52 else
54 Int32 intValue;
55 return Int32.TryParse(stringValue, out intValue);
59 /// <summary>
60 /// Gets a value indicating whether this validator supports browser validation.
61 /// </summary>
62 /// <value>
63 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
64 /// </value>
65 public override bool SupportsBrowserValidation
67 get { return true; }
70 /// <summary>
71 /// Applies the browser validation by setting up one or
72 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
73 /// </summary>
74 /// <param name="config">The config.</param>
75 /// <param name="inputType">Type of the input.</param>
76 /// <param name="generator">The generator.</param>
77 /// <param name="attributes">The attributes.</param>
78 /// <param name="target">The target.</param>
79 public override void ApplyBrowserValidation(BrowserValidationConfiguration config, InputElementType inputType,
80 IBrowserValidationGenerator generator, IDictionary attributes,
81 string target)
83 base.ApplyBrowserValidation(config, inputType, generator, attributes, target);
84 generator.SetDigitsOnly(target, BuildErrorMessage());
87 /// <summary>
88 /// Returns the key used to internationalize error messages
89 /// </summary>
90 /// <value></value>
91 protected override string MessageKey
93 get { return MessageConstants.InvalidIntegerMessage; }