0

我有一堆复选框,我将它们分组在 mx:FormItem 标记下,比方说称为 Application

<mx:FormItem  id ="application" label="Application" labelStyleName="labelStyle" width="520">

这些都需要验证。特别是,需要检查是否至少选择了这些复选框之一。这是在用户尝试提交按钮时完成的,所以我有一些效果:

<mx:Button width="60" label="Send" id="submit" click="doValidation();" />

doValidation() 函数有一堆条件需要检查,通过这些条件后,设置 Mx:FormItem 标记的 errorMessage 属性,如下所示

private function doValidation():void
{
  if(check.condition) //  check.condition is  a stub
     Application.errorString= "You messed up"; 
}

但是,不会显示此 errorString。我相信这是我卡住的地方。作为相对较新的 flex,我还不太清楚如何解决它。有人可以说明如何显示此错误消息。内部逻辑就好了,我已经调试了代码看看它是如何工作的,但是

4

1 回答 1

0

我不知道直接验证复选框的任何方法,但这里是一个手动创建的验证器的示例,用于在 FormItem 内找到多个复选框:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" 
    minWidth="955" minHeight="600"
    layout="vertical" 
    >

    <mx:Script>
        <![CDATA[
            private function doValidation():void{
                var itemsSelected:int = 0;

                for each(var checkBox:CheckBox in app.getChildren()){
                    if(checkBox.selected){
                        itemsSelected++;
                    }
                }

                if(itemsSelected == 0){
                    app.filters = [errorGlow];
                    app.toolTip = "You must select at least one value";
                }else{
                    app.filters = [];
                    app.toolTip = "";
                }
            }

            private function checkBoxClicked(e:Event):void{
                if((e.currentTarget as CheckBox).selected == true){
                    app.filters = [];
                    app.toolTip = "";
                }
            }
        ]]>
    </mx:Script>

    <mx:GlowFilter id="errorGlow"
        color="0xFF0000" blurX="5" blurY="5"
        />

    <mx:Form>
        <mx:FormItem id="app" 
            width="520"
            label="Application"
            >
            <mx:CheckBox id="checkBox1" label="Item 1" selected="false" click="checkBoxClicked(event)"/>
            <mx:CheckBox id="checkBox2" label="Item 2" selected="false" click="checkBoxClicked(event)"/>
            <mx:CheckBox id="checkBox3" label="Item 3" selected="false" click="checkBoxClicked(event)"/>
            <mx:CheckBox id="checkBox4" label="Item 4" selected="false" click="checkBoxClicked(event)"/>
            <mx:CheckBox id="checkBox5" label="Item 5" selected="false" click="checkBoxClicked(event)"/>
            <mx:CheckBox id="checkBox6" label="Item 6" selected="false" click="checkBoxClicked(event)"/>
            <mx:CheckBox id="checkBox7" label="Item 7" selected="false" click="checkBoxClicked(event)"/>
        </mx:FormItem>
    </mx:Form>
    <mx:Button  width="60" label="Send" id="submit" click="doValidation()" />

</mx:Application>
于 2012-10-24T20:20:29.120 回答