[英]How to create a custom method for a given customRule in Aurelia Validation
I am using aurelia-validation, and I created a customRule. 我正在使用aurelia-validation,我创建了一个customRule。
Rule validation logic: 规则验证逻辑:
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];
}
Configuration: 组态:
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 }));
}
Using the customRule: 使用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);
Question: 题:
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: 我正在使用typescript,我想创建一个包装使用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);
How can I create those requiredIf
and compare
methods and use it in the FluentRule? 如何创建requiredIf
并compare
方法并在FluentRule中使用它?
C# has extensions methods that it would be able to do, but I tried some ways in typescript without no success. C#有扩展方法可以做到,但我尝试了一些打字稿方法而没有成功。
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);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.