Flex:在验证失败时强制显示控件的errorTip(错误工具提示)

Jer*_*ell 3 apache-flex adobe flex3

当Validator(即StringValidator,NumberValidator等)由于验证失败而调度无效事件时,源控件的errorString属性(即TextInput)将设置为非空字符串,该字符串在控件周围创建一个红色边框并显示toolTip(errorTip)仅当鼠标悬停在控件上时.

问题:您是否可以强制立即显示toolTip(errorTip)而不是等待用户将鼠标悬停在控件上?如果是这样,怎么样?

ver*_*guy 7

Aral Balkan在zdmytriv的回答中链接的文章是良好的阅读,并提倡为用户提供更好的整体验证交互.

如果你只想"强制"弹出错误提示,这就是我的工作:

    public function showErrorImmediately(target:UIComponent):void
    {
        // we have to callLater this to avoid other fields that send events
        // that reset the timers and prevent the errorTip ever showing up.
        target.callLater(showDeferred, [target]);
    }

    private function showDeferred(target:UIComponent):void
    {
        var oldShowDelay:Number = ToolTipManager.showDelay;
        ToolTipManager.showDelay = 0;
        if (target.visible)
        {
            // try popping the resulting error flag via the hack 
            // courtesy Adobe bug tracking system
            target.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
            target.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
        }
        ToolTipManager.showDelay = oldShowDelay;
    }

    public function clearErrorImmediately(target:UIComponent):void
    {
        target.callLater(clearDeferred, [target]);
    }

    private function clearDeferred(target:UIComponent):void
    {
        var oldDelay:Number = ToolTipManager.hideDelay;
        ToolTipManager.hideDelay = 0;
        if (target.visible)
        {
            // clear the errorTip
            try
            {
                target.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
                target.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
            }
            catch (e:Error)
            {
                // sometimes things aren't initialized fully when we try that trick
            }
        }
        ToolTipManager.hideDelay = oldDelay;
    }
Run Code Online (Sandbox Code Playgroud)