This project is read-only.

Client Side Nullable Date Validation With Castle

Sep 30, 2009 at 6:21 PM
Edited Oct 1, 2009 at 5:30 PM


I'm new to xVal and Castle Validators and running into a problem.   

[ValidateNonEmpty("Order Date is required")]
[ValidateDate("Order Date is not a proper date")]
public DateTime OrderDate { get; set; }

ValidateDate("Ship Date is not a date")]
public DateTime? ShipDate { get; set; }

The OrderDate is working fine.  If no date is entered, the ValidateNonEmpty message appears.  If a non date value is entered, the ValidateDate message appears. 

The problem is with the ShipDate validation.  The goal is to allow an empty ShipDate or require a Date value when one is entered.  If ShipDate is marked as DateTime? , Castle will consider the ShipDate valid if it is null or if it contains a DateTime.   But xVal will treat a null ShipDate as invalid on the Client Side.

 I have also attempted to use the Remote Ajax call to solve the problem.  If I use DateTime? on the class,  the server side ShipDate.HasValue() returns false whether I enter "abc" or String.Empty.  So there is no way on the server to distinguish between an invalid date or no date.   If an invalid date were set to DatTime.Minium, I could check it that way.  But this is not happening.    If I use DateTime on the class , xVal always considers String.Empty to be an error.  So the user is not allowed to keep the ShipDate blank.

There are many .NET value types (integer, single, double, etc) that would also fail with similar requirements.  Can the current version of xVal handle this scenario or am I doing something wrong?



Dec 30, 2009 at 4:02 AM

I'm having the same problem with this.  I would think that unless you've set a field to be required then validation of that fields data type (e.g. integer, date, decimal etc..) should only happen if a value for the field has been entered.  It works this way for server side validation but not for client side.