简体   繁体   English

使用Aurelia对Blur进行现场验证

[英]Field validation onBlur with Aurelia

I'm using Aurelia and Typescript to build a web page. 我正在使用Aurelia和Typescript来构建一个网页。 I have a simple login form and I'd like to validate the user email and password. 我有一个简单的登录表单,我想验证用户的电子邮件和密码。

I am using Aurelia validation and by default it validates the content of my input each time it changes, which can be annoying. 我正在使用Aurelia验证,默认情况下,它会在每次更改时验证输入内容,这可能很烦人。 (ex: getting an error message saying that the email is not valid when you're not even done typing it). (例如:当您甚至没有输入时,收到一条错误消息,指出该电子邮件无效)。 So I'd like to do the validation onBlur instead (when the focus on the input is lost) and when the user clicks on the Login button. 所以我想在Blur上进行验证(当输入的焦点丢失时)以及当用户点击Login按钮时。

Here's my code: 这是我的代码:

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

My first thought was to add 我的第一个想法是补充

& updateTrigger:'blur':'paste'

to my binding in the HTML, but it doesn't work. 我在HTML中的绑定,但它不起作用。 The binding is updated correctly when focus is lost but the validation stops working. 当焦点丢失但验证停止工作时,绑定会正确更新。 There's no error in the Chrome debug console either. Chrome调试控制台中也没有错误。

Any idea on how to do this? 有关如何做到这一点的任何想法? Is is possible at all? 有可能吗?

There's different binding behaviours you can use to tell when the validation should trigger. 您可以使用不同的绑定行为来判断验证何时应该触发。 You can read more about them in the Aurelia docs on validation . 您可以在Aurelia文档中了解有关验证的更多信息。

From the docs; 来自文档;

The validate binding behavior obeys the associated controller's validateTrigger (blur, change, changeOrBlur, manual). 验证绑定行为服从关联控制器的validateTrigger(blur,change,changeOrBlur,manual)。 If you'd like to use a different validateTrigger in a particular binding use one of the following binding behaviors in place of & validate: 如果您想在特定绑定中使用不同的validateTrigger,请使用以下绑定行为之一代替&validate:

& validateOnBlur: the DOM blur event triggers validation. &validateOnBlur:DOM模糊事件触发验证。
& validateOnChange: data entry that changes the model triggers validation. &validateOnChange:更改模型的数据条目触发验证。
& validateOnChangeOrBlur: DOM blur or data entry triggers validation. &validateOnChangeOrBlur:DOM模糊或数据输入触发验证。
& validateManually: the binding is not validated automatically when the associated element is blurred or changed by the user. &validateManually:当关联元素被用户模糊或更改时,不会自动验证绑定。

I recently encountered this exact use case and I solved it using Aurelia's built in debounce feature. 我最近遇到了这个确切的用例,我使用Aurelia的内置去抖功能解决了它。

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

600ms is an arbitrary value, but you can always play around with it as needed. 600ms是一个任意值,但您可以随时根据需要使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM