【发布时间】:2017-08-04 10:09:37
【问题描述】:
我正在尝试对 Aurelia 组件进行基本的 Aurelia 验证,但出现错误。
打字稿是:
import { bindable, inject, NewInstance } from 'aurelia-framework';
import { HttpClient } from 'aurelia-fetch-client';
import { validationMessages, ValidationRules, ValidationController } from 'aurelia-validation';
@inject(HttpClient, NewInstance.of(ValidationController))
export class PersonDetail {
@bindable currentPerson: Person;
private httpClient: HttpClient;
public controller: ValidationController;
constructor(http: HttpClient, controller: ValidationController) {
this.httpClient = http;
this.controller = controller;
validationMessages['lastNameReq'] = 'You must enter a last name';
ValidationRules.ensure('LastName').minLength(3).required().withMessageKey('lastNameReq').on(this.currentPerson);
}
saveToDb() {
this.controller.validate()
.then(v => {
if (v.valid) {
// this.httpClient...(do stuff)
}
}
}
}
html 是:
<template bindable="currentPerson">
<form submit.delegate="saveToDb()">
<p>First Name: <input type="text" class="form-control" value.bind="currentPerson.FirstName"></p>
<p>Last Name: <input type="text" class="form-control" value.bind="currentPerson.LastName & validate"></p>
<p>Middle Name: <input type="text" class="form-control" value.bind="currentPerson.MiddleName"></p>
<p>Gender: <input type="text" class="form-control" value.bind="currentPerson.Sex"></p>
<p>DOB: <input type="text" class="form-control" value.bind="person.DOB"></p>
<p>Mobile Phone: <input type="text" class="form-control" value.bind="currentPerson.MobilePhnNbr"></p>
<p>Email: <input type="text" class="form-control" value.bind="currentPerson.EmailAddr"></p>
<button type="submit" class="btn-primary">Save to Database</button>
<ul if.bind="controller.errors">
<li repeat.for="error of controller.errors">
${error.message}
</li>
</ul>
</form>
</template>
运行这个我得到以下错误:
aurelia-logging-console.js:47 错误 [app-router] 类型错误: Object.defineProperty 在非对象上调用 在 Function.defineProperty() 在 Function.29.Rules.set (rules.js:14) 在 FluentEnsure.48.FluentEnsure.on (validation-rules.js:347) 在 FluentRuleCustomizer.48.FluentRuleCustomizer.on (validation-rules.js:95) 在 PersonDetail.app/components/people/person-detail.PersonDetail.bind (person-detail.ts:27) 在 View.bind (aurelia-template.js:1400) 在 Controller.bind (aurelia-template.js:3394) 在 View.bind (aurelia-templating.js:1406) 在 Controller.bind (aurelia-template.js:3394) 在 Controller.automate (aurelia-templating.js:3339)
如果我将 ValidationRules 行更改为:
ValidationRules.ensure('currentPerson.LastName').minLength(3).required().withMessageKey('lastNameReq').on(this);
然后我不再收到错误,但验证也不起作用(当我清空姓氏字段时 this.controller.validate() 返回有效)。这与 LStarky 在这里发现的内容相符:Aurelia Validation not working with object properties。但是,如果我实施他的解决方案,我会收到上述错误。
【问题讨论】:
-
this.curentPerson属性在ValidationRules.ensure行运行时是否未定义?我可能是错的,纯粹是根据错误猜测。 -
嗨@adiga 是的,没错,它是未定义的。我想出了下面的解决方案。
-
您是否尝试过将
ValidationRules应用于Person类本身,而不是该类的每个实例?像这样:ValidationRules.ensure('LastName').minLength(3).required().withMessageKey('lastNameReq').on(Person) -
我试过了,但是得到了一个打字稿错误 TS2693: 'Person' 只指一种类型,但在这里被用作一个值。 Aurelia 文档说您应该能够做到这一点。
-
好的 - 通过制作 Person 和导出类而不是 Interface 并将其导入 ts 文件来设法消除该错误。然而,验证没有工作! :-/
标签: javascript validation aurelia