Hi there,
I want to validate Date of Birth field which uses a UltraDateTimeEditor with following conditions:
1. If the user selects a Date of Birth greater than TODAY's date, shoud give an error "The date of birth must be before today"
I have written the following coding, but it doesn’t give the outputs that I needed.
=================================================================================================
this.udtDOB.NullText = "00/00/0000";
this.udtDOB.TabIndex = 24;
this.ultraToolTipManager1.SetUltraToolTip(this.udtDOB, ultraToolTipInfo2);
this.ultraValidator1.GetValidationSettings(this.udtDOB).DataType = typeof(System.DateTime);
this.ultraValidator1.GetValidationSettings(this.udtDOB).NotificationSettings.Caption = "Validation Faild";
this.ultraValidator1.GetValidationSettings(this.udtDOB).ValidationPropertyName = "Value";
this.udtDOB.Value = null;
// For the 2nd Condition
Urgent Help needed ! Thanks in Advance!
NW
The short answer to your question is that you will have to write code to do this. You could theoretically use two UltraValidators, one for a date range of two months ago through the current date, and another for the current date and later (the form'er error message would be a "warning" and the latter's an error), but I don't ever recommend using two validators on the same control.
The following code sample demonstrates how to use the ValidationError event to change the error message that is displayed to the end user based on how "far" the validated value is from some arbitrarily defined value, in your case, 2 months before the current date:
this.ultraDateTimeEditor1.NullText = "00/00/0000";this.ultraDateTimeEditor1.Value = null;ValidationSettings vs = this.validator.GetValidationSettings(this.ultraDateTimeEditor1);vs.ValidationPropertyName = "Value";DateTime maxDOB = DateTime.Today.AddMonths( -2 );vs.Condition = new DOBCondition( maxDOB );
public class DOBCondition : RangeCondition{ public DOBCondition( DateTime maxDOB ) : base(DateTime.MinValue, maxDOB, typeof(DateTime) ){}
public enum WarningType { None, Warning, Error }
public WarningType GetWarningType( DateTime value ) { DateTime maxDOB = (DateTime)this.MaximumValue; if ( value > DateTime.Today ) return WarningType.Error; else if ( value > maxDOB ) return WarningType.Warning; else return WarningType.None; }}
private void validator_ValidationError(object sender, Infragistics.Win.Misc.ValidationErrorEventArgs e){ ValidationResult result = e.Validation.Errors[0]; ValidationSettings vs = e.Validation.Errors[0].ValidationSettings; UltraDateTimeEditor dateTimeEditor = e.Control as UltraDateTimeEditor; if ( dateTimeEditor != null && vs.Condition is DOBCondition ) { DOBCondition condition = vs.Condition as DOBCondition; DOBCondition.WarningType warning = condition.GetWarningType( (DateTime)result.Value );
e.NotificationSettings.Caption = warning.ToString();
switch ( warning ) { case DOBCondition.WarningType.Warning: e.NotificationSettings.Text = "Warning : xxxxx!!"; break;
case DOBCondition.WarningType.Error: e.NotificationSettings.Text = "The date of birth must be before today"; break; } }}
Thanks alot Brain, It was really helpful.
But theres one more problem thats need to be figure out. When I add the above coding, it does not fire the message (e.g. "The date of birth must be before today") as soon as I pick the date from the Calendar. When I edit the textbox of the DateTimeEditor only I get the ultraValidator1_ValidationError fired.
I need the Messages to be fired as soon as I pick a date from the Calendar. I have added some extra coding thinking that it might work
(e.g. this.ultraValidator1.GetValidationSettings(this.udtDOB).ValidationTrigger = Infragistics.Win.Misc.ValidationTrigger.OnPropertyValueChanged;)
but it doesnt seems to work properly :(
Following shows my coding:
Designer
this.ultraValidator1.GetValidationSettings(this.udtDOB).IsRequired = true;
this.ultraValidator1.GetValidationSettings(this.udtDOB).ValidationTrigger = Infragistics.Win.Misc.ValidationTrigger.OnPropertyValueChanged;
this.ultraValidator1.ValidationError += new Infragistics.Win.Misc.ValidationErrorHandler(this.ultraValidator1_ValidationError);
In Form Load
this.ultraValidator1.SetValidationSettings(udtDOB, this.CreateOperatorCondition(this.udtDOB));
Methods
{
//Date of Birth validation settings
theDOBSettings.Condition = new DOBCondition(maxDOB);
}
ValidationSettings vs = e.Validation.Errors[0].ValidationSettings;
DOBCondition condition = vs.Condition as DOBCondition;
case DOBCondition.WarningType.Warning:
break;
e.NotificationSettings.Text = "The date of birth must be before today";
public class DOBCondition : RangeCondition
public enum WarningType
None,
Warning,
Error
if (value > DateTime.Today)
else
Please Help!! thanks