Skip to content


Flex 4.5 – Validating Spark Form Elements

I just started working with the new version of Flash Builder and Flex. I’m trying to use all Spark elements in order to get familiar with the new additions to the language. I ran into a problem when trying to use validators on Radio Buttons. After searching the web, I found almost nothing on how to deal with radio button validation. After experimenting and making a visit to the Adobe Flex forums, I was able to figure out what I was doing wrong.

With most form items, they can listen for the child elements validation error. However, with RadioButtons, it works a bit differently. You have to put the RadioButtons inside of a HGroup or VGroup and set that group as the RadioButton validator’s listener.

I have attached a working example below that will illustrate how to validate various elements in a form. View Source is enabled if you would like to download or view the code using Flex source view.

Visit my thread on this topic at Adobe Flex Forums.

 ActionScript 3 |  copy code |? 
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/08/30/displaying-the-error-indicator-on-a-spark-formitem-container-in-flex-hero/ -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:views="com.views.*"
               width="732" height="383" initialize="init();" name="Spark_Form_Validator_test"
               viewSourceURL="srcview/index.html">
 
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.controls.Alert;
            import mx.validators.Validator;
 
            [Binadble]private var myDP:ArrayCollection = new ArrayCollection();
            [Bindable]private var errors:Array;
 
            //Application Initilization
            private function init():void{
                var newItem:Object = new Object();
                newItem.data = 1;
                newItem.label = "Hello World";
                myDP.addItem(newItem);
            }
 
            //Validate the Validators Array
            private function validateForm():void{
                errors = new Array();
                errors = Validator.validateAll(validatorsArray);
                if (errors.length == 0){
                    Alert.show('All fields are valid.'); 
                }
                else{
                    Alert.show('Please complete the required fields.','See required fields');
                }
            }
 
            //Converts ComboBox text into and Object
            private function convertLabel(myString:String):Object{
                var comboObj:Object = new Object();
                comboObj.data = myComboBox.dataProvider.length + 1;
                comboObj.label = myString;
                return comboObj;
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>        
        <!--Array of Validators-->
        <fx:Array id="validatorsArray">
            <mx:StringValidator id="radioValidator" source="{group1}" property="selection" required="true" listener="{myGroup}"  />
            <mx:StringValidator id="textValidator" source="{myTextField}" property="text" required="true" />
            <s:NumberValidator id="dropdownValidator" source="{myDropDown}" property="selectedIndex" required="true" minValue="0" 
                               lessThanMinError="Please select an item"/>
            <mx:StringValidator id="comboValidator" source="{myComboBox}" property="selectedItem" required="true" listener="{myGroup2}"
                                requiredFieldError="Please enter or select an item." />
            <s:NumberValidator id="stepperValidator" source="{myStepper}" property="value" required="true" minValue="1" 
                               lessThanMinError="Minimum Value is 1"/>
        </fx:Array>
 
        <!--Radio Button Group-->
        <s:RadioButtonGroup id="group1" />
    </fx:Declarations>
 
<s:Panel x="38" y="17" width="660" height="338" title="Spark Validation Example">
    <s:Form id="form1" x="10" y="8" width="638" height="287">
        <s:layout>
            <s:FormLayout/>
        </s:layout>
 
        <!--Radio Button's _________________________________________________________-->
        <s:FormItem id="formItem1" width="100%" label="Validate Radio's:" required="true">
            <s:layout>
                <s:HorizontalLayout/>
            </s:layout>
 
            <s:HGroup id="myGroup">
                <s:RadioButton x="157" y="195" label="Selection 1" groupName="group1"/>
                <s:RadioButton x="157" y="221" label="Selection 2" groupName="group1"/>
                <s:RadioButton x="157" y="247" label="Selection 3" groupName="group1"/>
            </s:HGroup>
        </s:FormItem>
 
        <!--TextField________________________________________________________-->
        <s:FormItem  id="formItem2" width="100%" label="Validate TextField:" required="true">
            <s:TextInput id="myTextField" />
        </s:FormItem>
 
        <!--DropDownList________________________________________________________-->
        <s:FormItem id="formItem3" width="100%" label="Validate DropDownList:" required="true">
            <s:DropDownList id="myDropDown" width="155" labelField="label" prompt="---Choose One---">
                <s:dataProvider>
                    <s:ArrayCollection source="myDP"/>                
                </s:dataProvider>
            </s:DropDownList>
        </s:FormItem>
 
        <!--ComboBox________________________________________________________-->
        <s:FormItem width="100%" label="Validate ComboBox:" required="true">
            <s:HGroup id="myGroup2">
                <s:ComboBox id="myComboBox" labelField="label" labelToItemFunction="convertLabel">
                    <s:dataProvider>
                        <s:ArrayCollection source="myDP"/>
                    </s:dataProvider>
                </s:ComboBox>
            </s:HGroup>
        </s:FormItem>
 
        <!--Numeric Stepper________________________________________________________-->
        <s:FormItem width="100%" label="Validate NumericStepper:" required="true">
            <s:NumericStepper id="myStepper"/>
        </s:FormItem>
 
        <!--Validate Button________________________________________________________-->
        <s:FormItem width="100%">
            <s:layout>
                <s:HorizontalLayout/>
            </s:layout>
            <s:Button id="btnValidat" x="167" y="295" label="Validate Form" click="validateForm();"/>
        </s:FormItem>        
    </s:Form>    
</s:Panel>
 
</s:Application>

Posted in Technology.

Tagged with , , , , , , .


Search Engine Optimization with Google

A few years back I created a web page for K L Builders construction (www.klbuilderscps.com). It was a small website for a construction company based out of a small town. The website had been online for about three years and didn’t really have any external websites linking to it. Until about three weeks ago, the webpage sat dormant without any major updates or additions. I completed the customer’s request and ended up creating a brand new website. After I completed the changes, I decided to take some time and try to increase K L Builders rank in Google Places.

I setup the website with Google Analytics, submitted a sitemap to Google Webmaster Tools, and created business registry pages at Google Places, Merchant Circle and Yelp. I also followed Google guidelines for search engine optimization and added several meta tags to each page of the website. After I completed my work, I periodically checked the rank of K L Builders by use of Google Search and Google Webmaster tools. I was surprised to find that the old website was being cached and all the new pages were not showing up in the search results. I thought to myself, does Google not like K L Builders?

Continued…

Posted in Business, Technology.

Tagged with .


Create a Mint.com Style Login using Flex 4

I recently created an account on Mint.com. I was very impressed with their clean and functional layout, so I decided to create an example of how to create a Mint.com style login in Flex 4. The form below validates the text fields when the user focus out of that specific field. It throws an error tool tip when the user hovers off of the text field and also displays an error icon. I have supplied a working example and code snippet below. I would also like to give credit to FamFamFam for supplying me with the free icons!

Continued…

Posted in Technology.

Tagged with , , .


Filter a DataGrid as you Type in Flex 3

There are several different ways to filter a datagrid in Flex 3. I developed an easy dynamic function that will allow you to specify your dataprovider, datagrid name, columns to filter on, and filter string. Once you enter those four attributes, the function does the rest. When you enter a string inside of the text input, the filter function loops through the datagrid elements and filters the result accordingly. I have included a snippet of the filter function. If you wish to download this project, right click on the application and select view source. From the source viewer, you can download the project archive.
Continued…

Posted in Technology.

Tagged with , , , .


Repositioning an Alert in Flex 3

Being a programmer, I learned pretty quick that the little stuff is always what gets you. By default, Flex positions an Alert in the middle of the application. In some instances, you may want to specify the exact position of the Alert. In this example, I built a custom popup from the Alert class and use Flex’s PopUpManager to control the popup. By clicking the Pop Alert button in the example below, it will display the X and Y position of the PopUp. You can use the horizontal and vertical sliders to change the X and Y position. After doing so, you can click the Pop Alert button again to see the repositioned PopUp.

Continued…

Posted in Technology.

Tagged with , , , , , , .