2017-08-31 80 views
4

我正在使用aurelia-validation,并创建了一个customRule。如何为Aurelia中的给定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]; 
} 

配置:

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 })); 
} 

使用customRule:

ValidationRules 
    .ensure((m: ClienteEdicaoViewModel) => m.Login).required().satisfiesRule("login") 
    .ensure((m: ClienteEdicaoViewModel) => m.Senha).satisfiesRule("requiredIf", "ConfirmacaoSenha").satisfiesRule("senha") 
    .ensure((m: ClienteEdicaoViewModel) => m.ConfirmacaoSenha).displayName("Confirmação de Senha").satisfiesRule("requiredIf", "Senha").satisfiesRule("compare", "Senha") 
    .on(ClienteEdicaoViewModel); 

问:

我用的打字稿,我想创建一个包装使用satisfiesRule的方法,我想用这种方式来应用规则:

ValidationRules 
    .ensure((m: ClienteEdicaoViewModel) => m.Login).required().login() 
    .ensure((m: ClienteEdicaoViewModel) => m.Senha).requiredIf("ConfirmacaoSenha").senha() 
    .ensure((m: ClienteEdicaoViewModel) => m.ConfirmacaoSenha).displayName("Confirmação de Senha").requiredIf("Senha").compare("Senha") 
    .on(ClienteEdicaoViewModel); 

我怎样才能创建那些requiredIfcompare方法并在FluentRule中使用它?

C#有扩展方法,它可以做,但我尝试了一些方式在打字稿没有成功。

回答

4

您需要扩充验证模块并将实现提供给原型。 这就是你的配置应该是什么样子。

import { ValidationRules, validationMessages, FluentRuleCustomizer, FluentRules } 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 })); 
} 

declare module "aurelia-validation/dist/commonjs/implementation/validation-rules" { 
    interface FluentRules<TObject, TValue> { 
     compare(value: string): FluentRuleCustomizer<TObject, TValue>; 
    } 

    interface FluentRuleCustomizer<TObject, TValue> { 
     compare(value: string): FluentRuleCustomizer<TObject, TValue>; 
    } 
} 

FluentRules.prototype.compare = function (value: string) { 
    return this.satisfiesRule("compare", value); 
}; 

FluentRuleCustomizer.prototype.compare = function (value: string) { 
    return this.satisfiesRule("compare", value); 
}; 
+1

并感谢BTW的问题,它使我的代码更好了:)谢谢 –

+0

这么多,我用'声明模块“奥里利亚验证”',使用完整的位置,它的工作,很好地完成努力! ! –

+0

梦幻般的答案 –