我从http://aurelia.io/hub.html#/doc/article/aurelia/validation/latest/validation-basics/1
开始然后尝试http://blog.durandal.io/2016/06/14/new-validation-alpha-is-here/
但是,验证不会像预期的那样在模糊上发生。即使我在控制器上调用validate,也不会设置错误。
login.ts
import {inject, NewInstance} from 'aurelia-dependency-injection';
import {ValidationController} from 'aurelia-validation';
import {ValidationRules} from 'aurelia-validatejs'
@inject(NewInstance.of(ValidationController))
export class Login {
email:string;
password:string;
controller = null;
constructor(controller){
this.controller = controller;
}
submit = () => {
this.controller.validate().then((res) => console.log(res)).catch((e) =>console.log(e));
};
}
ValidationRules
.ensure('email').required()
.on(Login);
的login.html
<form submit.delegate="submit()">
<div class="form-group">
<label for="email" class="control-label required">Email</label>
<input class="form-control" value.bind="email & validate" type="email" id="email">
</div>
<div class="form-group">
<label for="password" class="control-label required">Password</label>
<input class="form-control" value.bind="password & validate" type="password"
id="password">
</div>
<div class="form-group">
<input id="remember_me" name="remember_me" type="checkbox" value="1">
<label for="remember_me" class="control-label">Remember me</label>
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit">Login</button>
</div>
</form>
aurelia.json
{
"name": "aurelia-validation",
"path": "../node_modules/aurelia-validation/dist/amd",
"main": "aurelia-validation"
},
{
"name": "validate.js",
"path": "../node_modules/validate.js",
"main": "validate"
},
{
"name": "aurelia-validatejs",
"path": "../node_modules/aurelia-validatejs/dist/amd",
"main": "aurelia-validatejs"
},
main.ts
aurelia.use
.standardConfiguration()
.plugin('aurelia-validation')
.plugin('aurelia-validatejs')
.feature('resources');
答案 0 :(得分:2)
您需要在类中创建验证规则,可能在attached
方法中,并且您希望传递this
而不是Login
。它应该是这样的:
attached(){
ValidationRules
.ensure('email').required()
.on(this);
}
答案 1 :(得分:2)
从版本0.12开始aurelia-validation
不再依赖validate.js
了。看起来您仍在使用已删除的依赖项中的ValidationRules
。只需更改
import {ValidationRules} from 'aurelia-validatejs'
到
import {ValidationRules} from 'aurelia-validation'
顺便说一下:您可以从项目依赖项中完全删除aurelia-validatejs
和validatejs
。
答案 2 :(得分:1)
我已经整理了一篇文章,解释了aurelia需要做的所有事情,以使验证正常运行。看起来你错过了拼图的渲染片段。如果这澄清了事情,请告诉我:
http://www.todroid.com/understanding-how-to-use-aurelias-validation-library/