如何在Aurelia验证中为给定的customRule创建自定义方法 [英] How to create a custom method for a given customRule in Aurelia Validation
问题描述
我正在使用aurelia验证,并创建了一个customRule.
I am using aurelia-validation, and I created a 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
用法的方法,我想以这种方式应用规则:
I am using typescript, and I would like to create a method that wraps the use of the satisfiesRule
, I would like to apply rules in this way:
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);
如何创建这些requiredIf
和compare
方法并将其在FluentRule中使用?
How can I create those requiredIf
and compare
methods and use it in the FluentRule?
C#具有扩展方法,但是我在打字稿中尝试了一些方法,但没有成功.
C# has extensions methods that it would be able to do, but I tried some ways in typescript without no success.
推荐答案
您需要扩展验证模块并将实现提供给原型. 这就是您的配置.
You need to augment the validation module and provide the implementation to the prototype. This is what your configuration should look like.
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);
};
这篇关于如何在Aurelia验证中为给定的customRule创建自定义方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!