2 May 2011
Exercise 1.1: Setting up Flash Builder and your project files
Beginning
In this exercise you will use the Flex DateTimeFormatter component to format the selected date from a calendar as shown in Figure 1. You will also learn how to format a phone number.
In this section you will create a DateTimeFormatter instance that will be used to format the date of the time entry.
Note the format of the date you selected in the Alert window (see Figure 2).
Declarations block.<fx:Declarations>
</fx:Declarations>
Declarations block, create a DateTimeFormatter instance.<fx:Declarations>
<s:DateTimeFormatter/>
</fx:Declarations>
DateTimeFormatter instance, add the id property with a value of requestDateFormatter and the dateTimePattern property with a value of MM-dd-yyyy.<s:DateTimeFormatter id="requestDateFormatter"
dateTimePattern="MM-dd-yyyy"/>
In this section you will use the DateTimeFormatter instance format the date displayed in the Alert window.
Script block, locate the dateChangeHandler() function.dateChangeHandler() function, to the Alert.show() method located outside the if statement, use the requestDateFormatter.format() method to format the event.result.selectedDate value.protected function dateChangeHandler(event:CalendarLayoutChangeEvent):void
{
Alert.show('You have selected ' + requestDateFormatter.format(event.target.selectedDate));
...
}
The date in the Alert window is formatted as specified by the DateTimeFormatter object (see Figure 3).
In this section you will create a PhoneFormatter instance that will be used to format the Office Phone and Mobile Phone fields of the Employee Portal Vehicle Request form.
Declarations block, below the DateTimeFormatter instance, create a PhoneFormatter instance.<fx:Declarations>
<s:DateTimeFormatter id="requestDateFormatter"
formatString="MM-dd-yyyy"/>
<mx:PhoneFormatter />
</fx:Declarations>
PhoneFormatter instance, add the id property with a value of phoneFieldFormatter, the formatString property assigned to (###) ###-####.<mx:PhoneFormatter id="phoneFieldFormatter"
formatString="(###) ###-####"/>
In this section you will use the PhoneFormatter instance to format the mobile phone after a number has been inserted.
id property of mobilePhone.focusOut event (see Figure 4).
focusOut event (see Figure 5).
Script block, locate the mobilePhone_focusOutHandler() function and delete the generated stub code.mobilePhone.text value, use the phoneFieldFormatter.format() method to format the number entered in the mobilePhone field.protected function mobilePhone_focusOutHandler(event:FocusEvent):void
{
mobilePhone.text = phoneFieldFormatter.format(mobilePhone.text);
}
DateChooser below the Mobile Phone field to remove focus from the field.The mobile phone number you entered is formatted as shown in Figure 6.
DateChooser to remove focus from the field.Note that this time the text you entered is deleted from the field. The formatter returns a blank field when the input is invalid. The only string that is valid is a 10 digit number without any characters.
Now you will add a custom validation check to return an Alert message if the input to the formatter is invalid.
mobilePhone_focusOutHandler() function.mobilePhone formatter, type if and press Ctrl+Space twice to show a list of code templates. Select the template for the if statement.phoneFieldFormatter.error as the condition of the statement.Alert.show() method to display the text Phone format error: followed by the error by using phoneFieldFormatter.error.if(phoneFieldFormatter.error)
{
Alert.show("Phone format error: " + phoneFieldFormatter.error);
}
DataChooser to remove focus.Note that the formatter still returns a blank field because the number you entered was invalid, but an alert message also appears indicating an invalid value (see Figure 7).
In this tutorial you learned how to use a DateFormatter class to display a date selected from a DateChooser control. You also learned how to use the PhoneFormatter class. In the next exercise you will use Flex framework validation classes to validate a form.