Aurelia验证匹配

时间:2015-12-09 11:30:36

标签: aurelia aurelia-validation

我想弄清楚如何做一个startsWith?这是一个自定义元素,它需要验证字符串以“\”

开头
<template>
    <input disabled.bind="readonly" type="text" class="form-control" value.bind="value">
</template>

import {customElement, bindable, inject, bindingMode} from 'aurelia-framework';
import {activationStrategy} from 'aurelia-router';
import $ from 'jquery';
import {Validation} from 'aurelia-validation';



@customElement('url')
@bindable({name: 'value', attribute: 'value', defaultValue: '', defaultBindingMode: bindingMode.twoWay})
@bindable({name: 'readonly', attribute: 'disabled', defaultValue: false, defaultBindingMode: bindingMode.oneWay})
@inject(Element, Validation)
export class Url {
    constructor(element, validation) {
        this.element = element;

        this.validation = validation.on(this)
                    .ensure(this.element)
                    .isNotEmpty()
                    .containsNoSpaces()
                    .matches('/^[\].*/');
    }

    bind(){
        $('.input', this.element).val(this.value);

        if(this.readonly){
            $('.input', this.element).attr('readonly', 'readonly');
        }
    }
}

我看过http://aurelia.io/validation/#/logical-operators并且我认为我做得对,但它会引发错误:内部错误:TypeError:path.split不是函数

1 个答案:

答案 0 :(得分:3)

  1. 函数ensure()接受验证字段的名称,而不是元素
  2. 无需用引号括起你的正则表达式
  3. 应该是

    this.validation = validation.on(this)
                             .ensure('value')   
                             .isNotEmpty()
                             .containsNoSpaces()
                             .matches(/^\\.*$/);