我正在尝试在表单上实施一些单元测试,以查看验证规则是否按预期工作。
从这个页面上:https : //github.com/aurelia/testing/issues/63
我发现了这个实现:https : //github.com/aurelia/validation/blob/master/test/validate-binding-behavior.ts
我试图在我的项目中实现它
login.spec.js
import {bootstrap} from 'aurelia-bootstrapper';
import {StageComponent} from 'aurelia-testing';
import {PLATFORM} from 'aurelia-pal';
import { configure, blur, change } from './shared';
import { Login } from './login';
describe('ValidateBindingBehavior', () => {
it('sets validateTrigger', (done) => {
const component = StageComponent
.withResources(PLATFORM.moduleName('features/account/login/login'))
.inView('<login></login>')
.boundTo({});
component.bootstrap(configure);
let viewModel;
const renderer = { render: jasmine.createSpy() };
component.create(bootstrap)
// grab some references.
.then(() => {
viewModel = component.viewModel;
viewModel.controller.addRenderer(renderer);
})
.then(() => expect(viewModel.controller.errors.length).toBe(0))
.then(() …Run Code Online (Sandbox Code Playgroud) 似乎其他地方在aurelia-validation模块方面存在各种问题,但我没有看到任何解决我遇到的具体问题的问题.
我有一个模型类,其定义和验证规则如下:
我-model.js
my-model = {
"name":
{
"full": "",
"short": "",
}
};
...
ValidationRules
.ensure(model => model.name.full).required().minLength(5).maxLength(50)
.on(this.my-model);
Run Code Online (Sandbox Code Playgroud)
但是,当我在浏览器中尝试它时,我收到错误:
...
Inner Error:
Message: Unable to parse accessor function:
function (model) {
return model.name.full;
}
...
Run Code Online (Sandbox Code Playgroud)
这个问题是我能够看到我最接近问题的问题,而另一个问题似乎也有同样的问题.
我正在aurelia-framework@^1.0.2和aurelia-validation@^1.0.0-beta.1.0.1我相信这些只是从定期更新(也为它的原因突然不工作)的默认设置.是否有可能我仍在运行某些模块的不兼容版本?或者我的代码中的其他地方是否需要修复?
我正在尝试在我的项目中设置aurelia-validation插件.我正在使用VS2015.我已经使用jspm install aurelia-validation命令安装了aurelia-validation插件.
Config.js文件已更新"aurelia-validation":"github:aurelia/validation@0.2.7",在jspm_packages/github中我看到了validation@0.2.7文件夹.
我在main.js文件中添加了一个插件.
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin('aurelia-validation');
aurelia.start().then(a => a.setRoot());
}
Run Code Online (Sandbox Code Playgroud)
并将我的aurelia-app更改为aurelia-app ="main"
当我启动应用程序时,我在Firefox控制台中收到以下错误:
Unhandled promise rejection TypeError: aurelia.globalizeResources is not a function
Stack trace:
configure@http://localhost:15724/jspm_packages/github/aurelia/validation@0.2.7/index.js:28:5
loadPlugin/<@http://localhost:15724/jspm_packages/github/aurelia/framework@0.15.0/aurelia-framework.js:34:32
run@http://localhost:15724/jspm_packages/npm/core-js@0.9.18/modules/es6.promise.js:91:43
notify/<@http://localhost:15724/jspm_packages/npm/core-js@0.9.18/modules/es6.promise.js:105:11
module.exports@http://localhost:15724/jspm_packages/npm/core-js@0.9.18/modules/$.invoke.js:6:25
@http://localhost:15724/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:40:9
run@http://localhost:15724/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:27:7
listner@http://localhost:15724/jspm_packages/npm/core-js@0.9.18/modules/$.task.js:31:5
Run Code Online (Sandbox Code Playgroud)
我试过用jspm cc命令清除jspm缓存,但这没有用.我的所有包裹都是最新的.
我错过了什么?
谢谢.
我正在使用aurelia-validation,我创建了一个customRule.
规则验证逻辑:
export function validateCompare(value: any, obj: any, otherPropertyName: string) {
return value === null ||
value === undefined ||
value === "" ||
obj[otherPropertyName] === null ||
obj[otherPropertyName] === undefined ||
obj[otherPropertyName] === "" ||
value === obj[otherPropertyName];
}
Run Code Online (Sandbox Code Playgroud)
组态:
import { ValidationRules, validationMessages } from "aurelia-validation";
import { validateCompare } from "./compareValidation";
export function configureValidation() {
validationMessages["required"] = "${$displayName} é obrigatório";
validationMessages["email"] = "${$displayName} em formato inválido";
ValidationRules.customRule("compare", validateCompare, "${$displayName} não confere com ${$getDisplayName($config.otherPropertyName)}", otherPropertyName => ({ otherPropertyName …Run Code Online (Sandbox Code Playgroud) 我正在尝试配置Aurelia验证(版本0.2.6)以将所有验证消息附加到<input>元素而不是标签.
我的main.js看起来像这样:
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging()
.plugin('aurelia-validation', (config) => { config.useLocale('de-DE').useViewStrategy(ValidateCustomAttributeViewStrategy.TWBootstrapAppendToInput); });
aurelia.start().then(a => a.setRoot('app', document.body));
}
Run Code Online (Sandbox Code Playgroud)
我总是得到以下错误消息:
未处理的承诺拒绝ReferenceError:未定义ValidateCustomAttributeViewStrategy
我究竟做错了什么?
我在Aurelia视图中有一个简单的选择列表,我试图在'Select ...'上设置默认值.我还使用aurelia-validation插件来确保在提交表单之前更改值.该插件非常适合我项目中的其他字段类型.
<div class="form-group">
<label for="agencies" class="control-label">Agency</label>
<select value.bind="agencyId" class="form-control">
<option value="">Select..</option>
<option repeat.for="agency of agencies" value.bind="agency.id">${agency.name}</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
在VM中:
constructor(validation) {
this.agencies = null;
this.agencyId = 0;
this.validation = validation.on(this)
.ensure('agencyId')
.isNotEmpty();
}
activate() {
//call api and populate this.agencies
}
Run Code Online (Sandbox Code Playgroud)
页面最初加载后,我在列表中获取我的代理,我的默认值是正确的,但它显示验证错误消息:

其他表单字段(如文本框)不会执行此操作,并且在用户与表单控件交互之前不会显示任何错误消息.
我是否需要为选择列表做一些特殊操作来隐藏视图初始加载时的验证错误?我怀疑在视图中绑定选择列表会以某种方式触发更改事件.
我创建了一个简单的文本框组件,它只包含一些样式以及标签和文本框的组合.当我尝试使用典型的绑定语法将验证绑定到此控件时,模糊功能不会导致验证执行.
组件视图如下所示:
<template>
<div class="form-field">
<div class="form-field__label">
${label}
<input name.bind="name" id.bind="name" class="form-field__input" type.bind="type" value.bind="value" tabindex.bind="tabIndex"></input>
</div>
</div>
</template>
Run Code Online (Sandbox Code Playgroud)
并且viewmodel看起来像:
import {bindable, bindingMode} from 'aurelia-framework';
export class Textbox {
@bindable({defaultBindingMode: bindingMode.twoWay}) value : string;
@bindable label : string;
@bindable type : string = "textbox";
@bindable tabIndex: number;
@bindable hidden : boolean;
@bindable name : string = '';
}
Run Code Online (Sandbox Code Playgroud)
组件使用如下:
<textbox value.bind="emailAddress & validate" type="email"></textbox>
Run Code Online (Sandbox Code Playgroud)
数据绑定按预期工作,但验证绑定不起作用.任何想法都表示赞赏.
我正在尝试使用aurelia和aurelia验证插件制作一些许可订阅表单.我有一个个人信息的字段集,其中大部分都是必需的,并通过aurelia验证进行验证.
现在我还有一个信用卡信息的字段集和帐单地址,以及需要和验证的字段.问题是,只有当用户选择订阅单选按钮时才会显示它们.我有ValidationRules中的所有必填字段,我怎么能告诉aurelia它应该只验证当前可见的字段?
我Aurelia-Validation在我的项目中使用并尝试验证电子邮件地址.当我添加电子邮件时example@g,它会通过验证.不该发电子邮件确认已.com,.net等扩展在年底通过验证?以plunker链接为例.
这是截图,显示我的意思:

我正在尝试遵循aurelia(验证)教程.但是,我知道它需要更新.即便如此,我认为通过文档,我将能够弄清楚如何使其工作,这是不会发生的.
原始代码如下:
import {inject} from "aurelia-framework";
import {Validation} from "aurelia-validation";
@inject(Validation)
export class Edit {
constructor(validation) {
this.validation = validation.on(this)
.ensure("movie.title")
.isNotEmpty()
.ensure("movie.releaseYear")
.isNumber();
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎不再起作用了.尝试在构造函数中进行验证时出现注入错误.
我去了文档,似乎不再存在验证符号.
我试图注入ValidationRules和验证到我的课,但没有支持保证或上的方法.
我尝试按照文档上的流程进行操作,其中"on"将是最后一个方法调用.但由于甚至没有确保工作,我被困住了.
感谢任何帮助.