Alb*_*iro 5 typescript aurelia aurelia-validation
我正在使用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)
使用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);
Run Code Online (Sandbox Code Playgroud)
题:
我正在使用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);
Run Code Online (Sandbox Code Playgroud)
如何创建这些requiredIf和compare方法并在FluentRule中使用它?
C#有扩展方法可以做到,但我尝试了一些打字稿方法而没有成功.
您需要扩充验证模块并为原型提供实现。\n这就是您的配置应该的样子。
\n\nimport { ValidationRules, validationMessages, FluentRuleCustomizer, FluentRules } from "aurelia-validation";\nimport { validateCompare } from "./compareValidation";\n\nexport function configureValidation() {\n validationMessages["required"] = "${$displayName} \xc3\xa9 obrigat\xc3\xb3rio";\n validationMessages["email"] = "${$displayName} em formato inv\xc3\xa1lido";\n\n ValidationRules.customRule("compare", validateCompare, "${$displayName} n\xc3\xa3o confere com ${$getDisplayName($config.otherPropertyName)}", otherPropertyName => ({ otherPropertyName }));\n}\n\ndeclare module "aurelia-validation/dist/commonjs/implementation/validation-rules" {\n interface FluentRules<TObject, TValue> {\n compare(value: string): FluentRuleCustomizer<TObject, TValue>;\n }\n\n interface FluentRuleCustomizer<TObject, TValue> {\n compare(value: string): FluentRuleCustomizer<TObject, TValue>;\n }\n}\n\nFluentRules.prototype.compare = function (value: string) {\n return this.satisfiesRule("compare", value);\n};\n\nFluentRuleCustomizer.prototype.compare = function (value: string) {\n return this.satisfiesRule("compare", value);\n};\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
214 次 |
| 最近记录: |