使用 Aurelia 对Blur 进行现场验证

Mat*_*ieu 5 html javascript validation typescript aurelia

我正在使用 Aurelia 和 Typescript 构建网页。我有一个简单的登录表单,我想验证用户电子邮件和密码。

我正在使用 Aurelia 验证,默认情况下它会在每次更改时验证我的输入内容,这可能很烦人。(例如:当您尚未输入电子邮件时,收到一条错误消息,指出电子邮件无效)。因此,我想在 Blur 上进行验证(当输入焦点丢失时)以及当用户单击“登录”按钮时。

这是我的代码:

登录.html

<template>
<section>
    <div class="container col-lg-12">
        <div class="col-md-4 col-md-offset-4 centered">
            <h2 t="signin_sign_in"></h2>
            <form role="form" submit.delegate="login()" validate.bind="validation">
                <br if.bind="errors" />
                <div if.bind="errors" repeat.for="error of errors" class="alert alert-danger">
                    <h4 repeat.for="message of error">${message}</h4>
                </div>
                <div class="form-group">
                    <label class="control-label" t="signin_email_address"></label>
                    <input type="text" class="form-control" value.bind="email">
                </div>
                <div class="form-group">
                    <label class="control-label" t="signin_password"></label>
                    <input type="password" class="form-control" value.bind="password">
                </div>
                <button type="submit" class="btn btn-primary" t="signin_sign_in"></button>
            </form>
        </div>
    </div>
</section>
</template>
Run Code Online (Sandbox Code Playgroud)

登录.ts

@autoinject()
export class Login {
  email: string;
  password: string;
  router: Router;
  application: ApplicationState;
  accountService: AccountService;
  errors;
  validation;
  i18n: I18N;

constructor(router: Router, application: ApplicationState, accountService: AccountService, validation: Validation, i18n: I18N) {
    this.router = router;
    this.application = application;
    this.accountService = accountService;
    this.i18n = i18n;
    this.errors = [];

    this.validation = validation.on(this)
        .ensure('email')
            .isNotEmpty()
            .isEmail()
        .ensure('password')
            .isNotEmpty()
            .hasLengthBetween(8, 100);
}

navigateToHome(): void {
    this.router.navigate("/welcome");
}

login(): void {
    var __this = this;
    this.validation.validate()
        .then(() => this.accountService.signin(this.email, this.password, this.rememberMe)
            .then(result => {
                // Do stuff
            })
            .catch(error => {
                // Handle error
                }
            }));
}
}
Run Code Online (Sandbox Code Playgroud)

我的第一个想法是添加

& updateTrigger:'blur':'paste'
Run Code Online (Sandbox Code Playgroud)

到我在 HTML 中的绑定,但它不起作用。当焦点丢失但验证停止工作时,绑定会正确更新。Chrome调试控制台也没有错误。

关于如何做到这一点有什么想法吗?有可能吗?

Kje*_*sen 0

您可以使用不同的绑定行为来判断何时触发验证。您可以在有关验证的 Aurelia 文档中阅读有关它们的更多信息。

来自文档;

验证绑定行为遵循关联控制器的 validateTrigger(blur、change、changeOrBlur、manual)。如果您想在特定绑定中使用不同的 validateTrigger,请使用以下绑定行为之一来代替 & validate:

& validateOnBlur:DOM模糊事件触发验证。
& validateOnChange:更改模型的数据输入会触发验证。
& validateOnChangeOrBlur:DOM 模糊或数据输入触发验证。
& validateManually:当关联元素模糊或被用户更改时,不会自动验证绑定。