Welcome to the Xceed Community | Help
Community Search  
More Search Options

DatePicker accepts Invalid Date

Sort Posts: Previous Next
  •  05-17-2012, 8:04 AM Post no. 32116

    DatePicker accepts Invalid Date

    Attachment: DatePicker Issue.zip

    Hi,

    We are facing one issue with DatePicker Control:-

    When user selects date from Control it works fine.But if selects date manually then issue occurs.

    If user enters invalid date manually (e.g. 99/99/9999) , it doesn't give any indication that invalid date has been entered.

    We want that whenever user enter invalid date manually then  Red Box should appear along the DatePicker with Tooltip (Invalid Date) to show that Invalid date has been selected. (Please find attached Sample Application & word file which explains the issue.)

    Please let us know how to achieve this.

    I have sent Sample Application with customized DatePicker which we use in our Main application. Please make changes in that sample application so it would be easier for us to replicate in our main application. 

    Regards, 

    Parthiv
  •  05-17-2012, 7:35 PM Post no. 32120 in reply to 32116

    Re: DatePicker accepts Invalid Date

    Hi Parthiv,

    You can also use VaildationRules if you can. You can use this to put specific date ranges by passing the date value chosen to this class and it will return whether the value is valid or not.

    If the months/year/day values are invalid such as a letter or the 44th month of the year, the DataCell will flash red and the DataGrid cannot exit out of edit mode because the date value is not valid. This is done because the DataGrid cannot cast the text in the DateTimeTextBox to a DateTime data type.

    You can refer to the 'Validation' sample application which can be found at
    C:\Xceed Samples\Xceed DataGrid for WPF Professional Edition vX.X\CSharp\Validation

    Here is the code for an example of how to code your own ValidationRule:

    C#
    ---------------
    public class YearValidationRule : ValidationRule
        {
            public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
            {
                if (value == null)
                    return new ValidationResult(false, "Chosen year cannot be null");

                DateTime date = (DateTime)value;

                if (date.Year > DateTime.Now.Year)
                    return new ValidationResult(false, "Chosen year cannot be greater than this year");

               

                return ValidationResult.ValidResult;
            }
        }
    ---------------


    Marc

    Developer in Technical Support
    Xceed - Multi-talented components - http://xceed.com
  •  05-21-2012, 7:31 AM Post no. 32131 in reply to 32120

    Re: DatePicker accepts Invalid Date

    Hi Marc,

    Thanks for your reply. Solution provided above is for DatePicker Control inside DataGrid but we are using DatePicker control directly.

    I have tried using ValidaionRule but when I enter invalid date such as "99/99/2012" then it doesn't get fired.

    Please can you make changes in sample application provided so it would be easier for us to replicate it in our main application.

    Thanks in advance.

     

    Regards,

    Parthiv 

  •  05-21-2012, 7:12 PM Post no. 32136 in reply to 32131

    Re: DatePicker accepts Invalid Date

    Hi Parthiv,

    You can add the ValidationRule to the DateTimeTextBox as an example and it will be called every time the control will lose focus. You can add binding to the DateTimeTextBox control if you need.

    public MainWindow()
            {
                this.DataContext = this;
                InitializeComponent();

                Binding testBinding = new Binding("DataContext.MyProperty");
                testBinding.ValidationRules.Add(new YearValidationRule());

                dttbTest.SetBinding(DateTimeTextBox.TextProperty, testBinding);
            }

            public DateTime MyProperty { get; set; }

    This should work for you so that way you can check the value of the DatePicker or the DateTimeTextBox


    Marc

    Developer in Technical Support
    Xceed - Multi-talented components - http://xceed.com
  •  06-19-2012, 6:46 AM Post no. 32269 in reply to 32136

    Re: DatePicker accepts Invalid Date

    Attachment: DatePickerSample.zip

    Hi Marc,

    Can you please implement the solution in attached sample application or provide us a demo application where above mentioned solution is implemented ?

    Thanks in advance.

     

    Regards,

    Parthiv 

  •  06-19-2012, 10:58 AM Post no. 32273 in reply to 32269

    Re: DatePicker accepts Invalid Date

    Hi Parthiv,

    Unfortunately, while we can provide answers in the form of explanations and code snippets, actually implementing those answers or providing custom sample projects are not within the scope of what is included in the support subscription.

    It is true that sometimes we take it on ourselves to send a small project sample if we think that it is easier that way to demonstrate an issue or solution, but that is an exception to the norm and is our decision. If you need a custom sample project for your specific needs, please submit a custom development request to the Sales team. Thank you.

     


    ** Quick Tip: Clients with an active support subscription should be sending their questions by email if they wish to benefit from the faster response time. Thanks!


    Diane Lafontaine
    Technical Support
    Xceed Software Inc.
  •  06-22-2012, 2:08 AM Post no. 32291 in reply to 32273

    Re: DatePicker accepts Invalid Date

    Hi,

    Can you provide us any reference where above mentioned solution has been already implemented ?

    Thanks in advance.

     

    Regards,

    Parthiv 

  •  06-22-2012, 9:18 AM Post no. 32292 in reply to 32291

    Re: DatePicker accepts Invalid Date

    Hi Parthiv,

    I would recommend looking at the Validation sample project that we include with our installation package.

     


    ** Quick Tip: Clients with an active support subscription should be sending their questions by email if they wish to benefit from the faster response time. Thanks!


    Diane Lafontaine
    Technical Support
    Xceed Software Inc.
  •  07-23-2012, 10:19 AM Post no. 32448 in reply to 32292

    Re: DatePicker accepts Invalid Date

    Hi,

    We have gone through the Validation sample project as suggested by you but over there the validation is applied to Column of DataGrid. Here we want a validation for DataPicker control itself as we are using the DatePicker as a stand alone control, not under any grid control.

    Is it possible to restrict user to enter only valid date, e.g if user is entering  44 number as a month then it should not get displayed in control. 

     Please provide the solution to this as soon as possible.

    Regards,
    Parthiv. 

  •  07-24-2012, 3:15 PM Post no. 32452 in reply to 32448

    Re: DatePicker accepts Invalid Date

    Hi Parthiv,

    You can do this by capturing the PreviewKeyboardLostFocus on the DatePicker to verify if the date is valid. If the date itself is invalid, then HasValidationError will be true. This is where you would add other validations if you wish, such as rejecting dates that are not in a specific range.

    Because the DatePicker is made of multiple elements (DateTimeTextBox, Calendar popup, etc) this event is called in several situations. So it is necessary to verify that the element about to receive the focus is outside the DatePicker first.

    For example:

       private void DatePicker_PreviewLostKeyboardFocus(object sender, System.Windows.Input.KeyboardFocusChangedEventArgs e)
       {
          var nf = e.NewFocus as DependencyObject;

          while ((nf != null) && (nf != sender))
          {
             var oldnf = nf;
             nf = VisualTreeHelper.GetParent(nf);

             if (nf == null)
                nf = LogicalTreeHelper.GetParent(oldnf);
          }

          if (nf == null)
          {
             Xceed.Wpf.Controls.DatePicker datePicker = sender as Xceed.Wpf.Controls.DatePicker;

             if (datePicker.HasValidationError)
             {
                // Date is invalid : change background color and force focus to stay (or other behaviors)
                datePicker.Background = Brushes.Red;
                e.Handled = true;
             }
             else
             {
                // other custom validations
                // ...

                // Date is valid : change background back to default
                datePicker.Background = null;
             }
          }
       }

     


    ** Quick Tip: Clients with an active support subscription should be sending their questions by email if they wish to benefit from the faster response time. Thanks!


    Diane Lafontaine
    Technical Support
    Xceed Software Inc.
View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.