Print Page | Close Window

How to verify that a date is in the future?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3244
Printed Date: 13-May-2026 at 10:48am


Topic: How to verify that a date is in the future?
Posted By: ajhops
Subject: How to verify that a date is in the future?
Date Posted: 31-Jan-2012 at 6:55am
What is the proper way to validate that a datetime*when set* is in the future? 

My field is "Publication Date". The business says that Publication Date must be today or after. This part is easy on the first save, but if the date passes and the user edits other fields on a day after publication - I get a server side validation error (but not client side). 

Silverlight if it matters.

Here is my static method to create this verifier:

public static Verifier GetFutureDateVerifier(Type type, string propertyName, bool isRequired)
		{
			var v = new DateTimeRangeVerifier(type, propertyName, isRequired, DateTime.Today.Date, DateTime.MaxValue);
			v.VerifierArgs.ErrorMessageInfo.ErrorMessage = "Must be on or after today.";
			return v;
		}

Thanks!!



Replies:
Posted By: sbelini
Date Posted: 01-Feb-2012 at 2:43pm
Hi ajhops,
 
I'd suggest enabling this validation for property updates only (i.e. disable it during Instance validation).
By default a verifier ExecutionMode is set to VerifierExecutionModes.InstanceAndOnBeforeSetTriggers. i.e. during instance validation and when the property is updated (OnBeforeSetTriggers).
You could disable it by set the ExecutionMode to VerifierExecutionModes.OnBeforeSetTriggers:
 
public static Verifier GetFutureDateVerifier(Type type, string propertyName, bool isRequired) {
  var v = new DateTimeRangeVerifier(type, propertyName, isRequired, DateTime.Today.Date, DateTime.MaxValue);
  v.VerifierArgs.ErrorMessageInfo.ErrorMessage = "Must be on or after today.";
  v.VerifierOptions.ExecutionModes =  VerifierExecutionModes.OnAfterSetTriggers;
  return v;
}
 
Note the if this property is required, you might want a RequiredValueVerifier to execute on instance validation.
 
You can find more information about configuring verifiers in the http://drc.ideablade.com/xwiki/bin/view/Documentation/validation-configure - DevForce Resource Center .
 
Regards,
   Silvio.



Print Page | Close Window