在Flex中显示FormItem的错误消息

时间:2012-10-24 18:30:28

标签: forms flex actionscript

我有一堆复选框,我在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,我还不太了解它。有人可以说明如何显示此错误消息。内部逻辑很好,我已经调试了代码以查看它是如何工作的,但是

1 个答案:

答案 0 :(得分: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>