Fixing an issue where setting a custom property on a handler will not propagate it...
[castle.git] / Components / General / Validator / Castle.Components.Validator / Validators / DateValidator.cs
blob99f918d41662f772a956027df68a61b622c4e7d5
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 DateValidator : AbstractValidator
27 /// <summary>
28 /// Checks if the <c>fieldValue</c> can be converted to a valid Date (so no time part).
29 /// Null and 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 DateTime datetimeValue;
41 bool valid = DateTime.TryParse(fieldValue.ToString(), out datetimeValue);
43 if (valid)
44 return IsDateOnly(datetimeValue);
46 return valid;
49 /// <summary>
50 /// Gets a value indicating whether this validator supports browser validation.
51 /// </summary>
52 /// <value>
53 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
54 /// </value>
55 public override bool SupportsBrowserValidation
57 get { return true; }
60 /// <summary>
61 /// Applies the browser validation by setting up one or
62 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
63 /// </summary>
64 /// <param name="config">The config.</param>
65 /// <param name="inputType">Type of the input.</param>
66 /// <param name="generator">The generator.</param>
67 /// <param name="attributes">The attributes.</param>
68 /// <param name="target">The target.</param>
69 public override void ApplyBrowserValidation(BrowserValidationConfiguration config, InputElementType inputType,
70 IBrowserValidationGenerator generator, IDictionary attributes,
71 string target)
73 base.ApplyBrowserValidation(config, inputType, generator, attributes, target);
75 generator.SetDate(target, BuildErrorMessage());
78 /// <summary>
79 /// Returns the key used to internationalize error messages
80 /// </summary>
81 /// <value></value>
82 protected override string MessageKey
84 get { return MessageConstants.InvalidDateMessage; }
87 /// <summary>
88 /// Check if only date given (so no time part)
89 /// </summary>
90 /// <param name="date">The date to check</param>
91 /// <returns><see langword="true"/>If Date only; otherwise, <see langword="false"/>.</returns>
92 private static bool IsDateOnly(DateTime date)
94 return (date.TimeOfDay.TotalMilliseconds == 0);