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>
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/flexcoders/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/