This Question is Answered

15 "helpful" answers available (3 pts)
2 Replies Last post: Jul 7, 2008 9:58 AM by fukuta

delete or backspace on a DateField

Jul 4, 2008 12:31 PM

Click to view fukuta's profile BlackBelt fukuta 118 posts since
Oct 17, 2007
I want to use delete and backspace keys to empty a DateField input.
DateField turns its value back to a last valid input when it is not parsable on ValueFinshed. But I'd rather it didn't when an input completely deleted or backspaced. Is it possible?
Click to view Kamal's profile Curl Kamal 149 posts since
Oct 17, 2007
1. Re: delete or backspace on a DateField Jul 5, 2008 7:02 AM
Here is an idea how to go about this. Note that in the given example below, as soon as the date will become invalid, the DateField value is unset. You can extend on this idea to only do it when you want. The best place to do this will be at the UI end as you may have more control. However you can do this by sub classing the control (the advantage is that, this will work for4 all UIs).

This solution takes advantage of the fact that the UI calls DateField.get-parsed-value to convert a String to a Date. If it cannot it throws a ValidationException.

I would suggest reading the source code of DateField and DateFieldUI(s) for constructing your own solution.


{curl 6.0 applet}
 
{define-class public CustomDateField {inherits DateField}  
  {constructor public {default
                          ui-object:#DateFieldUI = null,
                          min-value:#DateTime = null,
                          max-value:#DateTime = null,
                          value:#DateTime = null,
                          prompt:#String = null,
                          locale:#Locale = null,
                          calendar-control:CalendarControl = 
                              {CalendarControl takes-focus? = false},
                          ...
                      }
    {construct-super
        ui-object = ui-object,
        min-value = min-value,
        max-value = max-value,
        value = value,
        prompt = prompt,
        locale = locale,
        calendar-control = calendar-control,
        ...
    }
  }
  
  {method public {get-parsed-value
                     val:String,
                     require-four-digit-year?:bool = false
                 }:(date:DateTime, 
                    day-start-pos:int, day-end-pos:int, 
                    month-start-pos:int, month-end-pos:int, 
                    year-start-pos:int, year-end-pos:int)
    
    {try
        {return
            {super.get-parsed-value 
                val, 
                require-four-digit-year? = require-four-digit-year?
            }
        }
     catch e:ValidationException do
        {after 0s do
            {self.unset-value}
        }
        {throw e}
        
    }
  }
}
 
Original DateField: {DateField}
 
Custon DateField: {CustomDateField}
 
 


Message was edited by: Kamal
Click to view fukuta's profile BlackBelt fukuta 118 posts since
Oct 17, 2007
2. Re: delete or backspace on a DateField Jul 7, 2008 9:58 AM
in response to: Kamal
Thank you, Kamal.
I had been struggling with the open controls indeed, but I couldn't find a nice way. It went well by your advice.