Aurelia验证:对更改应用某些规则,对同一属性应用模糊规则 [英] Aurelia validation: applying some rule on change and some on blur on same property

查看:73
本文介绍了Aurelia验证:对更改应用某些规则,对同一属性应用模糊规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段,用于输入值,该值应正好为5位数字.我想立即输入除数字以外的其他字符时显示错误(onChange),但仅在模糊时显示字符串长度不足的错误.

I have an input field for a value that should have exactly 5 digits. I would like to show errors when typing characters other than digits immediatly (onChange) but showing the error for unsufficient string length only on blur.

目前我的规则如下:

ValidationRules
    .ensure("myInput")
    .matches(new RegExp(/[0-9]/))
    .minLength(5)
    .on(this);

通过在html中设置maxlength来限制MaxLength.

MaxLength is restricted by setting maxlength in html.

如果将验证触发器设置为"onChange"以立即响应错误的字符,则在键入正确的数字直到键入5个数字之前,如果不满足minLength规则,也会显示一条错误消息.

If I set the validation trigger to "onChange" to get an immediate response to wrong characters I also get an error shown for not satisfying the minLength rule while typing correct digits until having typed 5 of them.

我想拥有的行为是应用matchs-rule规则onChange和minLength-rule onBlur.

The behavior I would like to have is to apply the matches-rule onChange and the minLength-rule onBlur.

是否有可能在不同事件的同一属性上应用两个规则?我知道如何手动验证,但不知道如何区分规则.

Is there any possibility to apply two rules on the same property on different events? I know how to validate manually but I don't know how to differenciate between rules.

推荐答案

您可以使用when流利的API来满足您的需求.像这样-

You can use the when fluent API to satisfy your needs. Something like this -

ValidationRules
  .ensure('email')
    .email()
    .required()
      .when((order) => {
        if (order.length > 4) {
          order._ruleHasBeenMet = true;
        }
        return order.length > 4 && order._ruleHasBeenMet;
      }
      .withMessage('Email is required when shipment notifications have been requested.');

这篇关于Aurelia验证:对更改应用某些规则,对同一属性应用模糊规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆