返回顶部

收藏

验证Combo Boxes

更多
import mx.validators.ValidationResult;
    import mx.validators.Validator;
    public class ComboValidator extends Validator {
        // this is the error message that is returned if an item in the
        // ComboBox is not selected
        public var error:String;
        // if the developer sets a manual prompt, but pushes something into the
        // array of the ComboBox (I've seen it many times for different reasons)
        // we want to check that against what the selected item in the CB is
        public var prompt:String;
        public function ComboValidator() {
            super();
        }
        // here we check for either a null value or the possibility that
        // the developer has added a custom prompt to the ComboBox, in which
        // case we want to return an error
        override protected function doValidation(value:Object):Array {
            var results:Array = [];
            if(value as String == prompt || value == null) {
                var res:ValidationResult = new ValidationResult(true, "", "",
                                                                error);
                results.push(res);
            }

            return results;
        }
    }
}

<s:Group xmlns:fx="<a href="http://ns.adobe.com/mxml/2009">http://ns.adobe.com/mxml/2009"
         xmlns:s="<a href="library://ns.adobe.com/flex/spark">library://ns.adobe.com/flex/spark"
         xmlns:mx="<a href="library://ns.adobe.com/flex/halo">library://ns.adobe.com/flex/halo"
         width="600" height="400" xmlns:cookbook="oreilly.cookbook.flex4.*"
         creationComplete="init()">
    <fx:Declarations>
        <mx:StringValidator id="rbgValidator" source="{rbg}"
                            property="selectedValue"
                            error="Please Select a Radio Button"/>
        <mx:NumberValidator id="toggleValidator" source="{toggleButton}"
                            property="selected Index" allowNegative="false" />
        <cookbook:ComboValidator id="comboValidator" error="Please Select A State"
                                 prompt="{stateCB.prompt}" source="{stateCB}"
                                 property="selectedItem"/>
        <mx:RadioButtonGroup id="rbg"/>
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.events.ValidationResultEvent;
            import mx.validators.Validator;
            import mx.controls.Alert;
    [Bindable]
    private var validatorArr:Array;
    // make an array of all the validators that we'll check with one
    // method later
    private function init():void {
        validatorArr = new Array();
        // push all the validators into the same array
        validatorArr.push(rbgValidator);
        validatorArr.push(toggleValidator);
        validatorArr.push(comboValidator);
    }
    // validate all the items in the validator array and show an alert
    // if there are any errors
    private function validateForm():void {
        // the validateAll method will validate all the validators
           // in an array
           // passed to the validateAll method
           var validatorErrorArray:Array =
                   Validator.validateAll(validatorArr);
           var isValidForm:Boolean = validatorErrorArray.length == 0;
           if (!isValidForm) {
               var err:ValidationResultEvent;
               var errorMessageArray:Array = [];
               for each (err in validatorErrorArray) {
                   errorMessageArray.push(err.message);
               }
               Alert.show(errorMessageArray.join("\\n"), "Invalid form...
                          Alert.OK);
           }
       }
   ]]>
/fx:Script>
s:VGroup id="form">
   <mx:ComboBox id="stateCB" dataProvider="{someDataProvider}"
                prompt="Select A State"/>
   <s:HGroup>
       <s:RadioButton group="{rbg}" label="first" id="first"/>
       <s:RadioButton group="{rbg}" id="second" label="second"/>
       <s:RadioButton id="third" label="third" group="{rbg}"/>
   </s:HGroup>
   <s:ButtonBar id="toggleButton">
        <fx:dataProvider>
                <fx:ArrayList>
                    <fx:Number>1</fx:Number>
                    <fx:Number>2</fx:Number>
                    <fx:Number>3</fx:Number>
                </fx:ArrayList>
            </fx:dataProvider>
        </s:ButtonBar>
    </s:VGroup>
    <s:Button label="validate" click="validateForm()"/>
</s:Group>

//该片段来自于http://outofmemory.cn

标签:flash,ActionScript,安全

收藏

0人收藏

支持

0

反对

0

发表评论