ASTRA Form
Form is a UI component used for the layout and alignment of form elements. Like the Flex Form Class, it contains multiple vertically positioned component items with label arrangement determined by width. Additionally, it supports data collection and validation implemented in a corresponding validation and data manager class.
Using Form in Flash CS3
- Understanding Form class
- Layout Form
- Adding Items in a Form
- Setting styles
- Validating and storing form data
Understanding Form class
The primary purpose of the Form Class is to provide convenient visual alignment and data handling for HTML-like input forms. There are two classes that can be used in conjunction with the Form class to build an input form. The FormItem class handles visual alignment of form elements and the FormDataManager class manages form data.
- Form
Form is a layout element that contains multiple vertically arranged component items and the definition of each item, similar to the Flex Form. Any type of component or DisplayObject can be contained in a Form element. However, it is designed for efficient control of each FormItem's property and style (e.g. size, align) - FormItem
The FormItem container defines a label and one or more children arranged horizontally or vertically. It is designed to be nested in a Form rather than used as a standalone class. If you build Form with a data array, you would not have explicit use of it. - FormDataManager
Since the purpose of the form is collecting user input data, you may attachFormDataManagerto Form to accept and store the values.
Layout Form
Similar to the Flex Form container, Astra Form is usually used with nested FormItems and allows you to set styles for configuring the appearance of your form.
- Size setting
Form is an extension of
BoxPanewhich is a scrolling container with available horizontal and vertical scrollers. Therefore, If you specify the width and height properties, scroller(s) will show when needed based on the size of the your entire form. However, if the autoSize property is set true, the container will automatically calculate the idealwidthandheightfor itself without showing scrollers. Any attempts to set the width and height values manually while autoSize is set to true will be ignored.
or
labelAlign
All Form labels are right-aligned(
FormLayoutStyle.RIGHT). You can change the labels' alignment by setting thelabelAlignproperty to left(FormLayoutStyle.LEFT) and top(FormLayoutStyle.TOP) which stacks the labels vertically.


formHeading
FormHeading is an optional label field for Form that is displayed at the top of the form. It can be initialized in the constructor or can be set by the formHeading property.
or
Also, you can add items under the
FormHeadingby using thesubFormHeadingproperty. UnlikeFormHeading,subFormHeadingtakes not only a String but also multiple DisplayObjects.labelWidth
Form automatically matches the width of labels based on the longest label of all FormItems in the container. However, you can override the width of the label using the
labelWidthproperty. If the forced width is smaller than the actual label width, it will cut the label size according to the label alignment. (e.g., for the label aligned left(FormLayoutStyle.LEFT), right side of label will be delimited.)indicatorLocation
The location of the indicator (default is asterisk) for a required item can also be customized. The default location is between label and item(
FormLayoutStyle.INDICATOR_LABEL_RIGHT). Additional options for theindicatorLocationare left of label(FormLayoutStyle.INDICATOR_LEFT) and at the end of the item(FormLayoutStyle.INDICATOR_RIGHT).





- Control spacing : padding and gap
The padding properties allow you to specify how much space should appear between the edge of the Form and its content. The value of this attribute should be in specified in pixels rather than as percentage. Default padding is 0: no padding. The
horizontalGapproperty defines pixel gaps between labels and items.verticalGapdefines the gap between eachFormItem. itemHorizontalGapanditemVerticalGapdesignate the space between items.itemHorizontalGapdesignates the gaps for the items aligned horizontally(formItem.itemAlign=FormLayoutStyle.HORIZONTAL) in the FormItem, whileitemVerticalGapdoes the same for vertically aligned items(formItem.itemAlign=FormLayoutStyle.VERTICAL). See the example for the definition.
FormDataManager
FormDataManager is a set of methods to be used for collecting and validating data. See Validating and storing for details.
Adding Items to a Form
As seen in Getting Started, the easiest way to add items to a Form is to use a data array and pass it into dataSource. First, you must set data manager(formDataManager) in Form to handle the collected data, and than set the data property to an array which will collect the names and sources of data that will be applied to a Form. Let's see the possible data source in the data array.
- label : String
- items : Object or String(or Array of Object or String)
- itemAlign : String (default = "horizontal")
- instructionText : String(default = undefined)
- required : Boolean(default = false)
- itemHorizontalGap : Number(default = 6)
- itemVerticalGap : Number(default = 6)
- skin : DisplayObject(default = null)
- labelTextFormat : TextFormat(default = null)
- id
- source
- property
- validator
- validatorExtraParam
- errorString
- targetObj
- functionValidationPassed
- functionValidationFail
The following code shows a use of dataSource method in Form :
label
The label property generates a text label for the FormItem which takes only a String as its value. This label appears to the left of the child components of the FormItem. The position of the label is controlled by the
labelAlignproperty of Form. If you have an item that does not need a label, but want to align it with other items in the Form, set the label property to an empty string(" "). With no label attribute, Items will be aligned with the left border of the Form. See thesetStylefor text format of labels.items
The Items property takes an array of DisplayObject or String. Any string passed in quotation marks (e, g, "Zip Code") will be shown in the textField with same TextFormat as a label. By default, all items are left-aligned in the FormItem container. You can define the direction of multiple items by
itemAlign. The default alignment of multiple items is horizontal(FormLayoutStyle.HORIZONTAL)required
For visual presentation for a required field, you can specify the required property. Form inserts a red asterisk (*) character in the position that a
indicatorLocationhas specified. The required property takes a Boolean. For multiple items under one label, you need to specify the same number of required attributes in a FormItem. If the required property is specified, all of the children of the FormItem container are marked as required. In addition, you can change the skin of the indicator to your custom MovieClip by the setStyle property on the Form. See theindicatorSkinfor custom skin.itemAlign
When there are multiple items in a FormItem, by default, they will line up horizontally(
FormLayoutStyle.HORIZONTAL). You can change the direction to vertical by setting theitemAlignproperty's value to "vertical"(FormLayoutStyle.VERTICAL).

instructionTextEach FormItem can have an additional text label positioned under the item's field by setting a String value for the
instructionTextproperty. If the instructionText's width is longer than the item above, the width of the FormItem will reflect the width of the textfield. See thesetStyleforinstructionTextformatof labels.
- Using FormItem
If you have experience with the Flex Form container, you may know the use of a FormItem instead of using data array. The basic function of FormItem is similar to the FormItem class in Flex. You specify the label text and items to be containted in the FormItem constructor. To add this FormItem into a Form, apply the addItem property with optional "required" property value.
Setting styles
As an extended property of UIcomponent, You can use setStyle to set style property. However, calling this method can result in decreased performance. Use it only when necessary.
For more informaion, see defaultStyles at com.yahoo.astra.fl.containers.FormClasses.FormLayoutStyle class.
- Skins : skin, indicatorSkin
Setting "skin" style lets you use a custom Form background skin from the library.
To change the the required field indicator skin from the default(*), set "indicatorSkin" style to a custom movieClip from the library.
- TextFormat : textFormat,headTextFormat,instructionTextFormat
You can change all labels' text style in Form by setting the custom "textFormat" style. To customize
formHeadingandinstructionTextfields, set the style of "headTextFormat" and "instructionTextFormat", respectively. If you have a large number of FormItems and want them have other textFormats than the default, modifying FormLayoutStyle class is the preferred method to do this for optimal performance. - errorBoxColor, errorBoxAlpha
If
showErrorMessageBoxis settrueto visualize that the FormItem failed to pass validation, a gray transparent box will be shown under each formItem. Set style properties oferrorBoxColoranderrorBoxAlphato adjust color and alpha of the box. See more details fromshowErrorMessageBox.
Validating and storing form data
Form is mainly dedicated to the layout and alignment of forms. However, you can also use FormDataManager to collect user input data and validate it before you submit the data to the server. Astra does not provide a separate validation class, but there are compatible validation classes available from Adobe. See the Validator section for details.
To use FormDataManager in a Form, initialize and assign it to the Form before assigning the data array to dataSource. The FormDataManager constructor takes a value parser class(IValueParser) which will be used to collect user input. If your Form uses any CS3 UIcomponents, use the FlValueParser optimized for it, unless you want to apply your own custom value parser implementing IvalueParser.
To specify the data field to be stored, you can add the property below in the
dataSource array.
- id : String (required)
- source : Object (required)
- property : Object (default = null)
- required : Boolean (default = false)
- validator : Function (default = null)
- validatorExtraParam : Object (default = null)
- errorString : String(default = undefined)
- targetObj : DisplayObject (default = null)
- functionValidationPassed : Function (default = null)
- functionValidationFail : Function (default = null)
In addition, you can use FormDataManager as a standalone manager class dedicated to data collection and validation without attaching Form. FormDataManager collects attributes through the addItem or dataSource property. See the example below.
id
Since a
FormDataManagercollects and saves data in the form of associative arrays, "id" will be used as a property of the array(e.g. collectedData["zip"] = "94089"). Any property that has no designated id will be ignored. If you have multiple "id"s on one FormItem, you will need multiple instances of the following properties as well.(e.g. "source","property", "required",...)source, property
"source" points to the actual user input form objects. "property" specifies the name of the property of the source. For instance, if you have a TextInput component named as "nameInput" contained in FormItem by the "items" property, you can collect data in it when the user triggers the data collection. In this case, "nameInput" will be the "source" and "property" will be "text" by nature of TextInput. However, if you defined UIcomponent or textField as a "source", you don't need to specify the "property" unless you are expecting a non-standard result (e, g. If the
sourceis ComboBox, you don't have to specify its property, "data", as long as you want to retrieve data from it). See the ValueParser class and FlValueParser for full details.required, validator
The
requiredproperty has two roles. It verifies the visual presentation of a required field as well as enforces validation of the designated field. Therefore, if you have a field you want to enforce that the user fills with valid information, you need to define both "required" and "validator". A FormItem that has a "validator" but not a "required" set to "true" will be stored intoFormDataManager.collectedDatano matter if it has passed validation or not.Validation in FormDataManager is the process of checking that all required form items have been filled in correctly before the data is processed to the server. (For example, if your form has a TextInput field for a phone number, you might want the required input the user specifies to have the correct type.) The
validatoris a function property with a validation class and targeted method as an object. To use validation inFormDataManager, you should initialize the proper validation class and pass the desired function in the validator property. The validator takes a value of type Function in which the first argument is a String type(e.g. public function isEmail(str:String):as3ValidationResult {...}). The user input value will be passed into the function for verification and will be stored via the returned result. The current version of FormDataManager, supports the flash-validators provided by Adobe. For more details, refer to the Google code site : http://code.google.com/p/flash-validators. To use flash-validators,first import and initialize the flash-validators then point to the functionAnother option for the validation is the
mx.validatorsdistributed in the Flex SDK. For convenient use of Flex validators, you can use the AstraMXValidationHelperclass. Flexmx.validatorsprovides a variety of validation types and detailed error messages. However, the use of the Flex MXvalidator will increase your overall file size by approximately 20K.
- Adding a trigger
Unlike Flex Form, you can not add trigger validation on each FormItem. Instead, you can add a global trigger event for all validation and data storage. The
addTriggerfunction adds a listener on the displayObject's(first parameter)MouseEvent.CLICKevent, and defines a handler function for validation success(second parameter) and failure(third parameter).Below is the same process as addTrigger in the
FormDataManager. You can apply custom trigging action to callFormDataManager.collectData. - Data events
Once the data validation function is triggered,
FormDataManagercreates two Flash objects:FormDataManager.collectedDataandFormDataManager.failedData. The collected data form input fields will be added as a new key-value pair by creating a new property using the returned validation result. Values returned from validation will be collected inFormDataManager.collectedData(e.g. if an item that has "id" as "email" is passed the email validation, the value acquired from source will be stored incollectedDatain FormDataManager as : FormDataManager.collectedData["email"] = "address@yahoo.com").If required input fails validation, the error string will be stored infailedDatain FormDataManager(e.g. FormDataManager.failedData["email"] = "Missing an @ character in your email address."). This process continues for all of the items having "id" and "source" validated.If all of the required data passed the validation,
FormDataManagerwill dispatchFormDataManagerEvent.DATACOLLECTION_SUCCESS, otherwise, it will dispatchFormDataManagerEvent.DATACOLLECTION_FAIL. If there are defined attributes offunctionDataCollectionSuccessandfunctionDataCollectionFailinaddTriggerfunction, these functions will be triggered by the events. functionValidationPassed, functionValidationFail, eventTargetObj
By default, a FormItem having required parameter registers
FormDataManagerEventlisteners with type ofFormDataManagerEvent.VALIDATION_PASSEDandFormDataManagerEvent.VALIDATION_FAILEDtoeventTargetObj. You need to setfunctionValidationPassedandfunctionValidationFailto listener functions for these event types in theFormDataManagerbefore setdataSource.As an option, you can set
functionValidationPassedandfunctionValidationFailalong witheventTargetObjfor detailed result handling in each item.Any FormItem in a Form that is required with a validation will be assigned as an
eventTargetObjby default.showErrorMessageBox,showErrorMessageText
For visual presentation of error information for data that failed validation, Form provides two methods:
showErrorMessageText, andshowErrorMessageBox. TheshowErrorMessageTextproperty displays the returned error string under the targeted item. By settingshowErrorMessageBoxto true, each FormItems in Form will listen the validation events(FormDataManagerEvent.VALIDATION_PASSED,FormDataManagerEvent.VALIDATION_FAILED) and present a translucent gray box under the item that failed to validate. The default setting for theErrorMessageBoxis color: 0x666666 and alpha: 0.2.
SeesetStyleto set the property of the color and alpha of the gray box.


Both
as3DataValidationandMXValidatorsreturn error strings as the result of validation failure. However, for the results that have no error string defined in the validator, the default string, "Invalid input", is provided. You can change it to your custom message by settingerrorStringproperty.You can also specify
errorStringon each FormItem.
If showErrorMessageText is true and there is existing instructionText, the instructionText will be replaced by errorString.
![]() |
![]() |
For additional information, please take a look at the Examples section for functional demonstrations and the ActionScript 3.0 Class Reference for full details on every property, method, and styles available to the Form component.

