Fixing an issue where setting a custom property on a handler will not propagate it...
[castle.git] / Components / General / Validator / Castle.Components.Validator / Validators / NullableDateTimeValidator.cs
blobc9379ca574c51b1ec65ef11cc8c0d6a5017b85b8
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;
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 NullableDateTimeValidator : AbstractValidator
26 /// <summary>
27 /// Checks if the <c>fieldValue</c> can be converted to a valid DateTime.
28 /// Null or empty value 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) return true;
39 string stringValue = fieldValue.ToString();
41 if (stringValue == String.Empty) return true;
43 DateTime datetimeValue;
44 return DateTime.TryParse(stringValue, out datetimeValue);
47 /// <summary>
48 /// Gets a value indicating whether this validator supports browser validation.
49 /// </summary>
50 /// <value>
51 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
52 /// </value>
53 public override bool SupportsBrowserValidation
55 get { return true; }
58 /// <summary>
59 /// Returns the key used to internationalize error messages
60 /// </summary>
61 /// <value></value>
62 protected override string MessageKey
64 get { return MessageConstants.InvalidDateMessage; }