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 DateValidator
: AbstractValidator
28 /// If the <c>fieldValue</c> is not null, an attempt to convert the
29 /// content to a Date is performed, and the field is considered value
30 /// if the conversion is successful.
32 /// <param name="instance">The target type instance</param>
33 /// <param name="fieldValue">The property/field value. It can be null.</param>
35 /// <c>true</c> if the value is accepted (has passed the validation test)
37 public override bool IsValid(object instance
, object fieldValue
)
39 if (fieldValue
== null) return true;
43 DateTime date
= Convert
.ToDateTime(fieldValue
.ToString());
44 if (date
.TimeOfDay
.TotalMilliseconds
== 0)
57 /// Gets a value indicating whether this validator supports web validation.
60 /// <see langword="true"/> if web validation is supported; otherwise, <see langword="false"/>.
62 public override bool SupportsWebValidation
68 /// Applies the web validation by setting up one or
69 /// more input rules on <see cref="IWebValidationGenerator"/>.
71 /// <param name="config">The config.</param>
72 /// <param name="inputType">Type of the input.</param>
73 /// <param name="generator">The generator.</param>
74 /// <param name="attributes">The attributes.</param>
75 /// <param name="target">The target.</param>
76 public override void ApplyWebValidation(WebValidationConfiguration config
, InputElementType inputType
,
77 IWebValidationGenerator generator
, IDictionary attributes
, string target
)
79 base.ApplyWebValidation(config
, inputType
, generator
, attributes
, target
);
81 generator
.SetDate(BuildErrorMessage());
85 /// Returns the key used to internationalize error messages
88 protected override string MessageKey
90 get { return MessageConstants.InvalidDateMessage; }