0

我正在尝试对 mxml 文件中的三个字段进行验证。其中两个是必需的,一个不是。我只想在输入内容时验证邮政编码字段。如果它是空白的,那么我想允许启用“提交”按钮,只要正确设置了其他两个必填字段。代码:

<mx:EmailValidator id="emailValidator" 
                       property="text" source="{ownerName}"
                       valid="emailValidator_validHandler(event)"
                       invalid="emailValidator_validHandler(event)"/>
    <mx:ZipCodeValidator id="zipCodeValidator"
                         property="text" source="{zip}"
                         required="false"
                         valid="zipCodeValidator_validHandler(event)"
                         invalid="zipCodeValidator_validHandler(event)"/>

    <mx:Validator id="roomNameValidator" 
                  property="text" source="{roomName}"
                  valid="roomNameValidator_validHandler(event)"
                  invalid="roomNameValidator_validHandler(event)"/>
        [Bindable]
        private var isRoomNameValid:Boolean;
        [Bindable]
        private var isOwnerValid:Boolean;
        [Bindable]
        private var isZipValid:Boolean;

        protected function emailValidator_validHandler(event:ValidationResultEvent):void
        {
            isOwnerValid = (event.type==ValidationResultEvent.VALID);
        }

        protected function zipCodeValidator_validHandler(event:ValidationResultEvent):void
        {
            isZipValid = (event.type == ValidationResultEvent.VALID);
        }
        protected function roomNameValidator_validHandler(event:ValidationResultEvent):void
        {
            isRoomNameValid = (event.type==ValidationResultEvent.VALID);
        }

然后我尝试设置提交按钮的启用属性:

 enabled = "{isOwnerValid &amp;&amp; isRoomNameValid &amp;&amp zip.text.length>0?isZipValid:true}"

但它不起作用。即使邮政编码字段显示红色边框,我仍然可以在邮政编码字段中提交错误数据。

4

3 回答 3

0

这不能回答您的问题,但您可能需要认真考虑使用某种类型的 MVC。启用的函数中的所有逻辑都有代码味道。

于 2012-09-15T00:17:12.233 回答
0

编辑:我之前的建议可能导致无限循环:-)

我建议使用文本框的更改方法来执行验证,如下所示:

protected function changeHandler(event:Event):void
{
    submitButton.enabled = Validator.validateAll(validators.source);
}

<mx:ArrayCollection id="validators">
    <mx:EmailValidator id="emailValidator"
                   property="text" source="{ownerName}" />        
    <mx:ZipCodeValidator id="zipCodeValidator"
                   property="text" source="{zip}"
                   required="false" />
    <mx:Validator id="roomNameValidator" 
                   property="text" source="{roomName}" />
</mx:ArrayCollection>

<mx:Label text="Owner Name:" />
<mx:TextInput id="ownerName" change="changeHandler(event)" />
<mx:Label text="Zip Code:" />
<mx:TextInput id="zip" change="changeHandler(event)" />
<mx:Label text="Room Name:" />
<mx:TextInput id="roomName" change="changeHandler(event)" />
<mx:Button id="submitButton" label="Submit" enabled="false" />
于 2012-09-15T02:22:56.123 回答
0

首先,由于邮政编码不是强制性的,您应该将其初始化为true

[可绑定] var isZipValid:Boolean = true;


现在有一个解决方案:

protected function zipCodeValidator_validHandler(event:ValidationResultEvent):void
{
    isZipValid = (zip.text.length == 0) || (event.type == ValidationResultEvent.VALID);
}

然后在您的提交按钮中

enabled = "{isOwnerValid && isRoomNameValid && isZipValid}"

另一种解决方案是:仅当邮政编码不为空时才启用作为验证器本身,您应该将其初始化为 true :

<mx:ZipCodeValidator id="zipCodeValidator"
                 property="text" source="{zip}"
                 required="false"
                 valid="zipCodeValidator_validHandler(event)"
                 invalid="zipCodeValidator_validHandler(event)"
                 enabled="{zip.text.length > 0}"/>

[可绑定] var isZipValid:Boolean = true;

于 2012-09-15T03:15:56.993 回答