1 Reply Latest reply on Mar 12, 2010 4:41 PM by Peter deHaan

    Is there a Validator to mark a DropDownList as required?

    Paul The Lad

      Is there a Validator to mark a DropDownList as required? Or does anyone know of any web entries that talk about how to do this?

       

      I found something for RadioButtonGroup here: http://cookbooks.adobe.com/post_How_to_validate_radio_buttons_2-5962.html?w=rel

       

      But nothing for the spark DropDownList ... please help.

        • 1. Re: Is there a Validator to mark a DropDownList as required?
          Peter deHaan Level 4

          @The Lad,

           

          Short answer, use requiresSelection=true on the DropDownList control:

          <?xml version="1.0" encoding="utf-8"?>
          <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">
              <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>

           

           

          Long answer, use a NumberValidator and check that the DropDownList control's selectedIndex property is greater than -1:

          <?xml version="1.0" encoding="utf-8"?>
          <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">
              <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>

           

           

          Peter