validating properly in code behind

10 posts, 0 answers
  1. Brian
    Brian avatar
    18 posts
    Member since:
    Feb 2010

    Posted 07 Sep 2010 Link to this post

    I am validating a date in code behind using ParseDateTime event and setting the date back to a previous value if the validation failed but the DateTimeText value of the control is remaining at the invalid date that was selected.

    Here is my scenario:

    - Active date and Inactive date on page both bound to properties in datacontext.
    - Inactive date can be null but if it is set must be greater than active date.
    The following code works for the most part.  this._myDataContext.InactiveDate gets the correct value, inactiveDatePicker.SelectedDate gets the correct value but the date control still shows the invalid date. I need inactiveDatePicker.DateTimeText to be set to the proper value (or should display the watermark if _previousInactiveDate is null).
     
    void
     _InactiveDatePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
    {
        if (!ValidateDates())
        {
            this._myDataContext.InactiveDate = _previousInactiveDate;
        }
    }
















  2. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 09 Sep 2010 Link to this post

    Hi Brian,

    First of all I would recommend you to use the SelectedValue instead of the SelectedDate. Also the whole  validation code must be set in the ParseDateTimeValue event. In case you want to set your own parsing logic you can put the value to the ParseDateTimeEventArgs property. The WaterMarkContent will be displayed in case of null SelectedValue. Let us know if you need any further questions.

    Sincerely yours,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. Brian
    Brian avatar
    18 posts
    Member since:
    Feb 2010

    Posted 09 Sep 2010 Link to this post

    My logic was in the ParseDateTimeValue event handler. I do not want to do my own parsing logic, I just want to validate the date after it has been entered and if necessary revert it to a previous value.

    The following does seem to work. Is this what you meant?

            void _ActiveDatePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
            {
                if (!ValidateDates())
                {
                    _ActiveDatePicker.SelectedValue = _previousActiveDate;

                }
            }

  5. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 14 Sep 2010 Link to this post

    Hello Brian,

    Follow the recommended code bellow:

    private void radDateTimePicker_ParseDateTimeValue(object sender, Telerik.Windows.Controls.ParseDateTimeEventArgs args)
           {
               if (!args.IsParsingSuccessful && !ValidateDates())
               {
                   args.Result = previosDate;
                   args.IsParsingSuccessful = true;
               }
           }


    Kind regards,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Brian
    Brian avatar
    18 posts
    Member since:
    Feb 2010

    Posted 07 Oct 2010 Link to this post

    I believe it should be

    if (!args.IsParsingSuccessful && !ValidateDates())

    otherwise my ValidateDates method never gets called if the parsing was successful.
  7. Brian
    Brian avatar
    18 posts
    Member since:
    Feb 2010

    Posted 07 Oct 2010 Link to this post

    If I do the following in the ParseDateTime handler and this._OriginalObject.InactiveDate is null I get the exception listed below once control leaves the handler.

                if (args.IsParsingSuccessful && !ValidateDates())
                {
                    args.Result = this._OriginalObject.InactiveDate;
                    args.IsParsingSuccessful = true;
                } 
    Exception and call stack.
    [System.InvalidOperationException] {System.InvalidOperationException: Nullable object must have a value.
       at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
       at System.Nullable`1.get_Value()
       at Telerik.Windows.Controls.RadDateTimePicker.OnCurrentDateTimeTextChanged()
       at Telerik.Windows.Controls.RadDateTimePicker.OnCurrentDateTimeTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at Telerik.Windows.PropertyMetadata.<>c__DisplayClass1.<Create>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
       at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at Telerik.Windows.Controls.RadDateTimePicker.set_CurrentDateTimeText(String value)
       at Telerik.Windows.Controls.RadDateTimePicker.OnDateTimeTextChanged()
       at Telerik.Windows.Controls.RadDateTimePicker.OnDateTimeTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at Telerik.Windows.PropertyMetadata.<>c__DisplayClass1.<Create>b__0(DependencyObject d, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
       at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
       at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
       at Telerik.Windows.Controls.RadDateTimePicker.set_DateTimeText(String value)
       at Telerik.Windows.Controls.RadDateTimePicker.<UpdateText>b__5()} System.InvalidOperationException
  8. Brian
    Brian avatar
    18 posts
    Member since:
    Feb 2010

    Posted 08 Oct 2010 Link to this post

    Okay, I get why the exception. I am setting args.IsParsingSuccessful even if the date is null. If my previous date is null I will set IsParsingSuccessful to false.

    However, there is a bug in RadDateTime. If I set it to a value using args.Result = previousValue then the text portion of the date control does not change. If it set it to null the text portion of the control does not blank out.
  9. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 13 Oct 2010 Link to this post

    Hi Brian,

    Which version of the control are you using?

    Kind regards,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Brian
    Brian avatar
    18 posts
    Member since:
    Feb 2010

    Posted 13 Oct 2010 Link to this post

    2010.2.924.1040
  11. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 18 Oct 2010 Link to this post

    Hi Brian,

    I have made a small test project. It stores the old valid date entry and in case of an invalid input it restores the old valid. This means that the text will display the valid one. Am i missing something, as I may be not properly into your case.

    public partial class MainPage : UserControl
        {
            DateTime oldValue;
            public MainPage()
            {
                InitializeComponent();
            }
     
            private void radDateTimePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
            {
                if (!args.IsParsingSuccessful)
                {
                    args.IsParsingSuccessful = true;
                    args.Result = oldValue;
                }
                else
                {
                    oldValue = args.Result.Value;
                }
            }
        }


    Regards,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
DevCraft banner