aurelia-validation插件最近被重写,验证API已经在接受的答案方面再次发生了变化。
它现在使用2个独立的库aurelia-validation和aurelia-validatejs。验证程序似乎不再存在,并已由ValidationControllers取代。
新的API说明和一些例子可以在这里找到:
http://blog.durandal.io/2016/06/14/new-validation-alpha-is-here/
....和工作要点可以在这里找到:
https://gist.run/?id=381fdb1a4b0865a4c25026187db865ce
使用可以总结为以下代码:
import {inject, NewInstance} from 'aurelia-dependency-injection';
import {ValidationController, validateTrigger} from 'aurelia-validation';
import {required, email, ValidationRules} from 'aurelia-validatejs';
@inject(NewInstance.of(ValidationController))
export class RegistrationForm {
firstName = '';
lastName = '';
email = '';
constructor(controller) {
this.controller = controller;
// the default mode is validateTrigger.blur but
// you can change it:
// controller.validateTrigger = validateTrigger.manual;
// controller.validateTrigger = validateTrigger.change;
}
submit() {
let errors = this.controller.validate();
// todo: call server...
}
reset() {
this.firstName = '';
this.lastName = '';
this.email = '';
this.controller.reset();
}
}
ValidationRules
.ensure('firstName').required()
.ensure('lastName').required()
.ensure('email').required().email()
.on(RegistrationForm);
希望这有助于。
编辑:这已改变,显然validatejs是一个临时解决方案。
This article解释了它现在如何工作。如果你使用了validatejs,你还必须更新你的ValidationRenderer。此要点显示使用中的渲染器的更新版本:https://gist.run/?id=1d612b3ae341c7e9c12113e1771988e7
这是从博客未来如果链接死亡的代码片段:
import {inject, NewInstance} from 'aurelia-framework';
import {ValidationRules, ValidationController} from "aurelia-validation";
@inject(NewInstance.of(ValidationController))
export class App {
message = '';
firstname: string = '';
lastname: string = '';
constructor(private controller: ValidationController) {
ValidationRules
.ensure((m: App) => m.lastname).displayName("Surname").required()
.ensure((m: App) => m.firstname).displayName("First name").required()
.on(this);
}
validateMe() {
this.controller
.validate()
.then(v => {
if (v.length === 0)
this.message = "All is good!";
else
this.message = "You have errors!";
})
}
}
...和新的验证渲染器:
import {
ValidationRenderer,
RenderInstruction,
ValidationError
} from 'aurelia-validation';
export class BootstrapFormRenderer {
render(instruction) {
for (let { error, elements } of instruction.unrender) {
for (let element of elements) {
this.remove(element, error);
}
}
for (let { error, elements } of instruction.render) {
for (let element of elements) {
this.add(element, error);
}
}
}
add(element, error) {
const formGroup = element.closest('.form-group');
if (!formGroup) {
return;
}
// add the has-error class to the enclosing form-group div
formGroup.classList.add('has-error');
// add help-block
const message = document.createElement('span');
message.className = 'help-block validation-message';
message.textContent = error.message;
message.id = `validation-message-${error.id}`;
formGroup.appendChild(message);
}
remove(element, error) {
const formGroup = element.closest('.form-group');
if (!formGroup) {
return;
}
// remove help-block
const message = formGroup.querySelector(`#validation-message-${error.id}`);
if (message) {
formGroup.removeChild(message);
// remove the has-error class from the enclosing form-group div
if (formGroup.querySelectorAll('.help-block.validation-message').length === 0) {
formGroup.classList.remove('has-error');
}
}
}
}
希望这有助于!
我完全可以看到为Aurelia制作MS项目模板的价值。 Aurelia框架应用程序将400MB以上的文件安装到项目文件中,并且您必须运行命令行工具才能使任何内容工作。我习惯于让VS自动处理一切。我还不确定为什么人们认为NPM/JSPM的东西太棒了,但现在我会一起玩:)。 – Brian
我想看到两个VS项目,一个用于Aurelia Web应用程序,另一个用于Aurelia/Cordova应用程序。在我看来,这是两个最具商业化的场景。 –