Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / Components / Validator / Castle.Components.Validator / Validators / DateTimeValidator.cs
blob67180cb9703e61ce573aa4ce9e1372730047969c
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;
19 /// <summary>
20 /// This is a meta validator.
21 /// It is only useful to test a source content before setting it on the
22 /// target instance.
23 /// </summary>
24 public class DateTimeValidator : AbstractValidator
26 /// <summary>
27 /// Checks if the <c>fieldValue</c> can be converted to a valid DateTime.
28 /// Null or empty value are allowed.
29 /// </summary>
30 /// <param name="instance">The target type instance</param>
31 /// <param name="fieldValue">The property/field value. It can be null.</param>
32 /// <returns>
33 /// <c>true</c> if the value is accepted (has passed the validation test)
34 /// </returns>
35 public override bool IsValid(object instance, object fieldValue)
37 if (fieldValue == null || fieldValue.ToString() == "") return true;
39 DateTime datetimeValue;
40 return DateTime.TryParse(fieldValue.ToString(), out datetimeValue);
43 /// <summary>
44 /// Gets a value indicating whether this validator supports browser validation.
45 /// </summary>
46 /// <value>
47 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
48 /// </value>
49 public override bool SupportsBrowserValidation
51 get { return true; }
54 /// <summary>
55 /// Returns the key used to internationalize error messages
56 /// </summary>
57 /// <value></value>
58 protected override string MessageKey
60 get { return MessageConstants.InvalidDateMessage; }