1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 using System
.Collections
;
21 /// This is a meta validator.
22 /// It is only useful to test a source content before setting it on the
25 public class NullableDateValidator
: AbstractValidator
28 /// Checks if the <c>fieldValue</c> can be converted to a valid Date (so no time part).
29 /// Null or empty value allowed.
31 /// <param name="instance">The target type instance</param>
32 /// <param name="fieldValue">The property/field value. It can be null.</param>
34 /// <c>true</c> if the value is accepted (has passed the validation test)
36 public override bool IsValid(object instance
, object fieldValue
)
38 if (fieldValue
== null) return true;
40 string stringValue
= fieldValue
.ToString();
42 if (stringValue
== String
.Empty
) return true;
44 DateTime datetimeValue
;
45 bool valid
= DateTime
.TryParse(stringValue
, out datetimeValue
);
48 return IsDateOnly(datetimeValue
);
54 /// Gets a value indicating whether this validator supports browser validation.
57 /// <see langword="true"/> if browser validation is supported; otherwise, <see langword="false"/>.
59 public override bool SupportsBrowserValidation
65 /// Applies the browser validation by setting up one or
66 /// more input rules on <see cref="IBrowserValidationGenerator"/>.
68 /// <param name="config">The config.</param>
69 /// <param name="inputType">Type of the input.</param>
70 /// <param name="generator">The generator.</param>
71 /// <param name="attributes">The attributes.</param>
72 /// <param name="target">The target.</param>
73 public override void ApplyBrowserValidation(BrowserValidationConfiguration config
, InputElementType inputType
,
74 IBrowserValidationGenerator generator
, IDictionary attributes
,
77 generator
.SetDate(target
, BuildErrorMessage());
81 /// Returns the key used to internationalize error messages
84 protected override string MessageKey
86 get { return MessageConstants.InvalidDateMessage; }
90 /// Check if only date given (so no time part)
92 /// <param name="date">The date to check</param>
93 /// <returns><see langword="true"/>If Date only; otherwise, <see langword="false"/>.</returns>
94 private static bool IsDateOnly(DateTime date
)
96 return (date
.TimeOfDay
.TotalMilliseconds
== 0);