What if you used the ComboBox prompt property instead and only set the selectedIndex if you have a real value?  It might avoid the change event firing there too. 

Matt

 


From: [email protected] [mailto:[email protected]] On Behalf Of dwellet
Sent: Wednesday, June 22, 2005 1:43 PM
To: [email protected]
Subject: [flexcoders] bypass the change event

 

I have this combobox.

The combobox is populated by a webservice.

When the webservice result arrives, I have a utility function that
adds a 'Pick an item...' sort of option at the top of the dropdown,
and then selects the appropriate selected item.  If there is no
appropriate selected item, it picks index 0 (the dummy 'Pick an
item...').

I have a validator that validates that the combobox has a real
selection.

The problem is... I want my utility function to happen in "stealth"
mode. If the user changes the combobox, I want it to be validated. But
if I programatically set the selectedIndex, I don't want any events to
fire and trigger validation.

Ironically, I read that in Flex 1.0, changing the selectedIndex did
not fire a change event, but this was "fixed" in 1.5. The old behavior
is precisely what I want. Any way to temporarily bypass that change
event firing?

Code included below... Thanks for your help!

-Tim.




[ComboBoxValidator.as]


import mx.controls.*;
import mx.validators.*;

/* ComboBoxValidator
*
* Validate that a user does not select the dummy option (eg.
* 'Pick a value...') in a required combobox.
*
*/
class ComboBoxValidator extends Validator
{
   public function doValidation(foo:String) : Void
   {
      var n = Number(foo);
      if (n == 0)
      {
         validationError("noSelection", "Field is required.", null);
      }
   }
}





[Test.mxml]


<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
                xmlns:dom="*"
                creationComplete="start()">

    <mx:Script>
    <![CDATA[
        import mx.controls.*;
        import mx.containers.*;
        import mx.managers.*;
        import mx.validators.*;

        import com.dom.sampling.vo.*;

        var model : SampleSite;

        var PICK_A_FACILITY_TYPE : FacilityType;

        /* start()
         *
         * Perform basic setup.
         *
         */
        function start()
        {
            PICK_A_FACILITY_TYPE      = new FacilityType();
            PICK_A_FACILITY_TYPE.name = "Choose a facility type...";
            service.getAllFacilityTypes();

            if (!model)
            {
                model = new SampleSite();
            }
        }

        /* prepareCombobox()
         *
         * Prepares a combobox by adding a first, descriptive
         * option (eg. 'Pick a value...') to the dropdown. It
         * also sets the initial value of the combobox, either
         * to the item matching the passed in initialValue, or
         * if no match is found, to the first item in the
         * combobox.
         *
         * @param combobox      the combobox to prepare
         *
         * @param pickAValue    the first, descriptive option
         *                      (eg. 'Pick a value...') to add
         *                      to the combobox
         *
         * @param initialValue  the initialValue the combobox
         *                      should be set to
         */
        public function prepareComboBox(combobox: ComboBox,
                                        pickAValue: Object,
                                        initialValue: Object) : Void
        {
            if (pickAValue)
            {
                combobox.addItemAt(0, pickAValue);
                combobox.selectedIndex = 0;
            }

            if (initialValue)
            {
                for (var i=0; i < combobox.length; i++)
                {
                    var item = combobox.getItemAt(i);

                    // object
                    if (item instanceof Object &&
                        item.id == initialValue.id)
                    {
                        combobox.selectedIndex = i;
                    }

                    // non-object
                    else if (item == initialValue)
                    {
                        combobox.selectedIndex = i;
                    }
                }
            }
        }

    ]]>
    </mx:Script>

    <mx:WebService id="service"
                   wsdl="/sampling-ws/services/LocationService?wsdl"
                   showBusyCursor="true">

        <mx:operation name="getAllFacilityTypes"
                      result="prepareComboBox(facilityType,
PICK_A_FACILITY_TYPE, model.facilityType)"/>

    </mx:WebService>

    <mx:Panel title="Administer Sample Site" width="100%">
        <mx:FormItem width="100%" required="true" label="Facility
Type">
            <mx:ComboBox id="facilityType"
dataProvider="{service.getAllFacilityTypes.result}"
labelField="name"/>
        </mx:FormItem>
    </mx:Panel>

    <mx:Model id="validationModel">
        <facilityType>{facilityType.selectedIndex}</facilityType>
    </mx:Model>

    <dom:ComboBoxValidator required="true"
field="validationModel.facilityType"/>

</mx:Application>





--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




Yahoo! Groups Links

Reply via email to