使用Aurelia对Blur进行现场验证

问题描述 投票:4回答:2

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

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

这是我的代码:

的login.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>

login.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
                }
            }));
}
}

我的第一个想法是补充

& updateTrigger:'blur':'paste'

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

有关如何做到这一点的任何想法?有可能吗?

javascript html validation typescript aurelia
2个回答
0
投票

您可以使用不同的绑定行为来判断验证何时应该触发。你可以在Aurelia docs on validation上阅读更多相关信息。

来自文档;

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

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


0
投票

我最近遇到了这个确切的用例,我使用Aurelia的内置去抖功能解决了它。

<input type="text" class="form-control" id="email" placeholder="Email" value.bind="email & validateOnChangeOrBlur & debounce:600 ">

600ms是一个任意值,但您可以随时根据需要使用它。

© www.soinside.com 2019 - 2024. All rights reserved.