The following example shows how you can check whether the user has selected an item in the Spark DropDownList control in Flex 4 by setting the Boolean requiresSelection property to true or using a NumberValidator to check the DropDownList control’s selectedIndex property is greater than -1.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?> 
<!-- http://blog.flexexamples.com/2010/03/12/checking-that-an-item-is-selected-in-the-spark-dropdownlist-control-in-flex-4/ -->
<s:Application name="Spark_DropDownList_requiresSelection_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"> 
    <s:layout>
        <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <s:DropDownList id="ddl" prompt="Please select an item" requireSelection="true">
        <s:dataProvider>
            <s:ArrayList source="[one,two,three,four,five,six,seven]" />
        </s:dataProvider>
    </s:DropDownList>
 
</s:Application>

View source is enabled in the following example.

Or you can use a NumberValidator on the Spark DropDownList control to check that the selectedIndex property is greater than or equal to 0, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/03/12/checking-that-an-item-is-selected-in-the-spark-dropdownlist-control-in-flex-4/ -->
<s:Application name="Spark_DropDownList_selectedIndex_NumberValidator_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"> 
    <s:layout>
        <s:HorizontalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.events.ValidationResultEvent;
 
            protected function button1_clickHandler(evt:MouseEvent):void {
                numValidator.validate();
            }
 
            protected function numValidator_validHandler(evt:ValidationResultEvent):void {
                Alert.show("DropDownList item selected: " + ddl.selectedItem.toString(), evt.type);
            }
 
            protected function numValidator_invalidHandler(evt:ValidationResultEvent):void {
                Alert.show(ddl.prompt, evt.type);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <mx:NumberValidator id="numValidator"
                source="{ddl}"
                property="selectedIndex"
                minValue="0"
                lowerThanMinError="{ddl.prompt}"
                valid="numValidator_validHandler(event);"
                invalid="numValidator_invalidHandler(event);" />
    </fx:Declarations>
 
    <s:DropDownList id="ddl" prompt="Please select an item">
        <s:dataProvider>
            <s:ArrayList source="[one,two,three,four,five,six,seven]" />
        </s:dataProvider>
    </s:DropDownList>
 
    <s:Button label="validate" click="button1_clickHandler(event);" />
 
</s:Application>

View source is enabled in the following example.

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

One Response to Checking that an item is selected in the Spark DropDownList control in Flex 4

  1. Derrick Grigg says:

    For DropDownList checking selectedIndex = -1 for validation works great, on a ComboBox you need to check for >=0 because a user defined value shows up as selectedIndex -3

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree