Fix for the Datefield issue in flex 2.0

Sunday, 22 July 2007, 12:09 | Category : Flex
Tags :

There seems to be an issue with the <mx:DateField> component in Flex 2.0. The issue is that when you choose todays date i.e. the current date which is highlighted grey in the DateChooser again the selected date clears out and you get a null as the selected date. Even if you select any other date two times in the chooser its becomes null and the text clears out. I am not sure why on earth Adobe is clearing out the date. Let the user select any date he wants to and let programmer’s brain do the rest 😉 . This is an issue and the workaround i coded for the above is as follows.

Let’s take an example:

<mx:DateField id=”dfStartDate” selectedDate=”{DateRange.getDate(xxx)}”
editable=”true” change=”updateStartDate(event)”/>

private function updateStartDate(event : CalendarLayoutChangeEvent) : void {
if (event.newDate != null) {
var dateNew : Date = new Date(event.newDate.getFullYear(), event.newDate.month, event.newDate.date);
dfStartDate.selectedDate = event.newDate;
}
}

This function gets the changed date and sets it back to the dateField so that we do not get null on getting the selectedDate and also the datefield does not clear out.flex date field

But the problem i am facing now is whenever the user changes the date the text in the datefield shifts left for no good reason and the date text is not readable. i have to again focus in the field to make it readable. Please suggest a workaround.

Aaah !!! i finally managed to fix the date field width issue by setting the width to 75 i.e. just set the width=”75″ or greater and the value will not shift to left.