在 Flex 中的多个控件上使用相同的验证器

发布于 2024-07-13 04:07:00 字数 132 浏览 4 评论 0原文

假设我有一个 Flex 中的电话号码验证器,并且有两个用于电话号码的 TextInput 控件。 我不想定义两个具有基本相同属性的单独验证器控件...但每个验证器只有一个“源”属性。 如何在多个控件上使用相同的验证器? (或任何等效的解决方案)

Say I have a phone-number validator in flex and I have two TextInput controls for phone numbers. I don't want to have two separate validator controls defined that have essentially the same attributes... but each validator has only one "source" attribute. How can I use the same validator on multiple control? (or any equivalent solution)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是我的挚爱i 2024-07-20 04:07:00

不是内联的,但您可以以编程方式执行验证,例如在提交表单时或当用户按 Tab 键移出控件时等。下面是一个使用单个 PhoneNumberValidator 来验证两个表单字段的示例; 单击“提交”按钮时会进行验证:

<mx:Script>
    <![CDATA[

        private function validatePhoneNumber(txt:TextInput):void
        {
            v.listener = txt;
            v.validate(txt.text);
        }

        private function btn_click():void
        {
            validatePhoneNumber(p1);
            validatePhoneNumber(p2);
        }

    ]]>
</mx:Script>

<mx:PhoneNumberValidator id="v" allowedFormatChars="()- .+" property="text" requiredFieldError="Required." wrongLengthError="Invalid length." invalidCharError="Invalid character." />

<mx:Form>
    <mx:FormItem label="Phone Number 1">
        <mx:TextInput id="p1" />    
    </mx:FormItem>
    <mx:FormItem label="Phone Number 2">
        <mx:TextInput id="p2" />    
    </mx:FormItem>
    <mx:FormItem>
        <mx:Button id="btn" label="Submit" click="btn_click()" />
    </mx:FormItem>
</mx:Form>

希望有帮助!

Not inline, but you can perform the validation programmatically, say, on submission of a form, or when a user tabs out of a control, etc. Here's an example using a single PhoneNumberValidator to validate two form fields; the validation happens when the Submit button gets clicked:

<mx:Script>
    <![CDATA[

        private function validatePhoneNumber(txt:TextInput):void
        {
            v.listener = txt;
            v.validate(txt.text);
        }

        private function btn_click():void
        {
            validatePhoneNumber(p1);
            validatePhoneNumber(p2);
        }

    ]]>
</mx:Script>

<mx:PhoneNumberValidator id="v" allowedFormatChars="()- .+" property="text" requiredFieldError="Required." wrongLengthError="Invalid length." invalidCharError="Invalid character." />

<mx:Form>
    <mx:FormItem label="Phone Number 1">
        <mx:TextInput id="p1" />    
    </mx:FormItem>
    <mx:FormItem label="Phone Number 2">
        <mx:TextInput id="p2" />    
    </mx:FormItem>
    <mx:FormItem>
        <mx:Button id="btn" label="Submit" click="btn_click()" />
    </mx:FormItem>
</mx:Form>

Hope it helps!

半暖夏伤 2024-07-20 04:07:00

重现步骤:

  1. TextInput 动态创建

    textInputBox = new MyTextInput; 
      textInputBox.restrict = “0-9。”; 
      textInputBox.maxChars = 24; 
      amountValidator = new NumberValidator(); 
      amountValidator.source = textInputBox; 
      amountValidator.property = “文本”; 
      amountValidator.allowNegative = false; 
      amountValidator.domain = “真实”; 
      amountValidator. precision = 4; 
      amountValidator.required = false; 
      amountValidator.maxValue = 999999999999.9999; 
      amountValidator.trigger = textInputBox; 
      amountValidator.triggerEvent = Event.CHANGE; 
      amountValidator.addEventListener(ValidationResultEvent.VALID, amountValid); 
      amountValidator.addEventListener(ValidationResultEvent.INVALID, amountInvalid); 
    
      私有函数 amountValid(event:ValidationResultEvent):void 
      { 
          有效=真; 
          字段验证 = true; 
      } 
    
      私有函数 amountInvalid(事件:ValidationResultEvent):void 
      { 
          有效=假; 
          字段验证 = true; 
      } 
      
  2. 正如创建中提到的,当我们超过限制时,它会显示错误我的红色边框,同时如果您在达到给定的可接受限制时通过 DEL 键删除它们,很快就会自动变绿。
  3. 离开该字段并更改另一个文本输入的值(这只是一个文本输入,这是一个表单,还有更多表单元素),然后按 SHIFT+TABS 返回超出值的文本字段并删除额外输入的数字,当您来时很快你的价值就被接受了。
  4. 现在再次输入更多值,现在您处于警告区域,然后离开该字段并对其他表单元素进行一些更改。
  5. 然后回到通过鼠标单击提交的超出值的文本,并开始从 DEL 删除,即使您删除了其他值,仍然字段显示您处于警告区域。

实际结果:
即使删除附加数字,字段仍然为红色

预期结果:
如果删除附加数字,字段应恢复正常状态。

此问题的图片可以在查看屏幕截图中查看

Steps to reproduce:

  1. TextInput creates dynamically

    textInputBox = new MyTextInput;
    textInputBox.restrict = “0-9.”;
    textInputBox.maxChars = 24;
    amountValidator = new NumberValidator();
    amountValidator.source = textInputBox;
    amountValidator.property = “text”;
    amountValidator.allowNegative = false;
    amountValidator.domain = “real”;
    amountValidator.precision = 4;
    amountValidator.required = false;
    amountValidator.maxValue = 999999999999.9999;
    amountValidator.trigger = textInputBox;
    amountValidator.triggerEvent = Event.CHANGE;
    amountValidator.addEventListener(ValidationResultEvent.VALID, amountValid);
    amountValidator.addEventListener(ValidationResultEvent.INVALID, amountInvalid);
    
    private function amountValid(event:ValidationResultEvent):void
    {
        valid = true;
        fieldsValidated = true;
    }
    
    private function amountInvalid(event:ValidationResultEvent):void
    {
        valid = false;
        fieldsValidated = true;
    }
    
  2. As mention in the creation, when we exceed the limit, it shows error my red color border, and the same time if you delete them by DEL key when it come to the given acceptable limit, automatically become to green soon.
  3. Leave from the field and change values of another textinput(this is just a textinput, this is a form there are some more form elemets), then come back to value exceeded textfield by SHIFT+TABS and remove the additional entered numbers, when you come to green soon your value is accepted.
  4. Now again enter more values and now you are in the warn zone, then leave from the field and do the few changes in other form elements.
  5. Then come back to the value exceeded text filed by MOUSE CLICK, and start delete from DEL, even though you removed additional values, still fields shows that you are in warn zone.

Actual Results:
Even when remove additional numbers,still field is Red

Expected Results:
if remove additional numbers, field should come its normal status.

Picture of this issue can be viewed at View Screen Shot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文