2 May 2011
Exercise 1.1: Setting up Flash Builder and your project files
Beginning
In this exercise you will use Flex framework validation classes to validate a form (see Figure 1). You will start by validating a single field and then learn how to validate an entire form using ActionScript.
In this section you will add an asterisk to all form fields to indicate that the fields are required.
FormItem block that nests the mobilePhone TextInput control.FormItem container, add the required property with a value of true.<s:FormItem label="Mobile Phone:" required="true">
<s:TextInput id="mobilePhone"/>
</s:FormItem>
You should see an asterisks next to the Mobile Phone field (see Figure 2).
In this section, you will add help content to provide context and instructions for using a form item.
FormItem container, above the TextInput control, add a helpContent tag block.Note that you can put anything you want in the helpContent container. In this exercise, you will add a Label control and a Button control.
helpContent container.text property with a value of (ex. 555-555-5555).helpContent tag block, add a Button control with a label property of ?, a width property that has a value of 30, and an x property that has a value of 120.<s:helpContent>
<s:Label text="(ex. 555-555-5555)"/>
<s:Button label="?"
width="30"
x="120"/>
</s:helpContent>
You should see that the Label and Button controls have been added to the form, but notice that neither control lines up on the same horizontal baseline as the Mobile Phone field (see Figure 3).
baseline property with a value of 24. <s:helpContent>
<s:Label text="(ex. 555-555-5555)" baseline="24"/>
<s:Button label="?" width="30" x="120" baseline="24"/>
</s:helpContent>
You should see that the Label and Button controls are now aligned properly with the Mobile Phone field (see Figure 4).
In this section you will create a PhoneNumberValidator instance that will be used to validate the Mobile Phone field of the sample application.
Declarations block.DateTimeFormatter, add a PhoneNumberValidator instance.<fx:Declarations>
<s:DateTimeFormatter id="requestDateFormatter"
formatString="MM-dd-yyyy"/>
<mx:PhoneNumberValidator />
</fx:Declarations>
id property with a value of mobileValidator.<mx:PhoneNumberValidator id="mobileValidator"/>
In this section you will use a Button control's click event to trigger the validation of the Mobile Phone field.
Declarations block, to the PhoneNumberValidator instance, add the source property and bind its value to the mobilePhone TextInput control.<mx:PhoneNumberValidator id="mobileValidator"
source="{mobilePhone}"/>
property property with a value of text.<mx:PhoneNumberValidator id="mobileValidator"
source="{mobilePhone}"
property="text"/>
trigger property with a value that is bound to the submitButton Button control.<mx:PhoneNumberValidator id="mobileValidator"
source="{mobilePhone}"
property="text"
trigger="{submitButton}"/>
triggerEvent property set to click.<mx:PhoneNumberValidator id="mobileValidator"
source="{mobilePhone}"
property="text"
trigger="{submitButton}"
triggerEvent="click"/>
You should see a red border appear around the mobilePhone TextInput control. You should also see that the required indicator changes to the error indicator and the contents of the helpContent container is replaced with text telling you the field is required (see Figure 5).
<mx:PhoneNumberValidator id="mobileValidator"
source="{mobilePhone}"
property="text"
triggerEvent=""/>
private function validatePhones():void
{
}
validatePhones() function, create a local variable named event that is data typed as a ValidationResultEvent.Note that if you use the content assist tool to assign the data type, Flash Builder generates the corresponding import statement. Otherwise you must add it manually.
private function validatePhones():void
{
var event:ValidationResultEvent
}
event variable to call the mobileValidator PhoneNumberValidator instance's validate() method and append a semicolon.private function validatePhones():void
{
var event:ValidationResultEvent = mobileValidator.validate();
}
FormItem container with the nested Button control.click event with a value of the validatePhones() event handler.<s:FormItem>
<s:Button id="submitButton"
label="Submit Request"
click="validatePhones()"/>
</s:FormItem>
You should see that the application behaves exactly as it did in Figure 5.
In this section, you will use ActionScript and Flex validators to validate both Phone fields in the Company Vehicle Request component at once.
Declarations block, copy the PhoneNumberValidator instance and paste a copy below the first.<mx:PhoneNumberValidator id="mobileValidator"
source="{mobilePhone}"
property="text"
triggerEvent=""/>
<mx:PhoneNumberValidator id="mobileValidator"
source="{mobilePhone}"
property="text"
triggerEvent=""/>
PhoneNumberValidator instance, change the id value to officePhoneValidator and bind the source value to the phone TextInput control.<mx:PhoneNumberValidator id="officePhoneValidator"
source="{phone}"
property="text"
triggerEvent=""/>
Script block.import statement to import the mx.validators.Validator package.import events.VehicleRequestEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CalendarLayoutChangeEvent;
import valueObjects.VehicleRequest;
import mx.events.ValidationResultEvent;
import mx.validators.Validator;
validatePhones() function.validatePhones() function, delete the event variable and its value.validatePhones() function, declare a variable named validationArray and data type to the Array class.private function validatePhones():void
{
var validationArray:Array
}
validationArray variable to call the Validator.validateAll() method.private function validatePhones():void
{
var validationArray:Array = Validator.validateAll();
}
Validator.validateAll() method, use array brackets to create an array of validators. List the mobileValidator and the officePhoneValidator instances for validation.private function validatePhones():void
{
var validationArray:Array = Validator.validateAll([mobileValidator,officePhoneValidator]);
}
Both TextInput controls turn red and a message displays next to the Office Phone field indicating the error as shown in Figure 6.
In this exercise you learned how to create a PhoneNumberValidator instance and use it to validate a form field upon a button click. In the next exercise you will populate a ColumnChart control bound to value object instances stored in the employees ArrayCollection instance.
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.