如何在客户端构建用于 Castle Validation 的自定义验证器?
我正在使用城堡验证,我想知道为什么我的验证器不起作用:
[Serializable]
public class PositiveIntegerValidator : AbstractValidator
{
public override bool IsValid(object instance, object fieldValue)
{
if (fieldValue == null || !(fieldValue is int))
return false;
return ((int)fieldValue) > 0;
}
public override bool SupportsBrowserValidation
{
get { return true; }
}
public override void ApplyBrowserValidation(BrowserValidationConfiguration config, InputElementType inputType, IBrowserValidationGenerator generator, System.Collections.IDictionary attributes, string target)
{
base.ApplyBrowserValidation(config, inputType, generator, attributes, target);
generator.SetValueRange(target, 0,int.MaxValue, ErrorMessage);
}
protected override string BuildErrorMessage()
{
return ErrorMessage;
}
}
public class ValidatePositiveIntegerAttribute : AbstractValidationAttribute
{
public ValidatePositiveIntegerAttribute(string msg) :base(msg){}
public override IValidator Build()
{
PositiveIntegerValidator positiveIntegerValidator = new PositiveIntegerValidator();
ConfigureValidatorMessage(positiveIntegerValidator);
return positiveIntegerValidator;
}
}
我的字段
public class PackageViewModel
{
// ...
[ValidateNonEmpty,ValidatePositiveInteger("The package count must be positive")]
public int nbPackage { get; set; }
//...
}
和我的视图
$FormHelper.TextField("package.nbPackage","%{size='3',value='1'}")
ValidateNonEmpty 在客户端和服务器端都进行验证,但 ValidatePositiveInteger 则不然。
我看过这个帖子 Min Length Custom AbstractValidationAttribute and Implementing Castle.Components。 Validator.IValidator ,但我看不出我的代码和他的代码有什么区别。
I'm using castle validation and I'd like to know why my validator is not working :
[Serializable]
public class PositiveIntegerValidator : AbstractValidator
{
public override bool IsValid(object instance, object fieldValue)
{
if (fieldValue == null || !(fieldValue is int))
return false;
return ((int)fieldValue) > 0;
}
public override bool SupportsBrowserValidation
{
get { return true; }
}
public override void ApplyBrowserValidation(BrowserValidationConfiguration config, InputElementType inputType, IBrowserValidationGenerator generator, System.Collections.IDictionary attributes, string target)
{
base.ApplyBrowserValidation(config, inputType, generator, attributes, target);
generator.SetValueRange(target, 0,int.MaxValue, ErrorMessage);
}
protected override string BuildErrorMessage()
{
return ErrorMessage;
}
}
public class ValidatePositiveIntegerAttribute : AbstractValidationAttribute
{
public ValidatePositiveIntegerAttribute(string msg) :base(msg){}
public override IValidator Build()
{
PositiveIntegerValidator positiveIntegerValidator = new PositiveIntegerValidator();
ConfigureValidatorMessage(positiveIntegerValidator);
return positiveIntegerValidator;
}
}
And my field
public class PackageViewModel
{
// ...
[ValidateNonEmpty,ValidatePositiveInteger("The package count must be positive")]
public int nbPackage { get; set; }
//...
}
And my view
$FormHelper.TextField("package.nbPackage","%{size='3',value='1'}")
The ValidateNonEmpty validate on both client and server side, but the ValidatePositiveInteger is not.
I've seen this thread Min Length Custom AbstractValidationAttribute and Implementing Castle.Components.Validator.IValidator , but I can't see any difference between my code and his.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在浏览 Castle Validation 源代码后终于找到了它:
默认验证是 PrototypeWebValidator,该验证器使用 PrototypeValidationGenerator 进行客户端验证,并且当您调用“SetValueRange”时此实现不会执行任何操作:
所以这似乎是合乎逻辑的,我的客户端验证不起作用(PrototypeValidationGenerator 没有实现此功能主要是因为底层验证 API https://github.com/atetlaw/Really-Easy-Field-Validation 没有'也不要实施它)。
所以我决定扩展它,因为它是一个框架,也是框架的目的:提供一个框架并让您在其中工作。
所以我创建了生成器
验证器提供了一些用于添加自定义验证的功能
formhelper 所需的验证器:
然后将城堡连接到基本控制器上的验证器,如下所示:
我有一个 BaseController,但这个解决方案不是最好的,但我不知道如何将其注入城堡单轨列车中配置。
我会尝试将其提交到 Castle Monorail 的源代码库......
I finally found it after browsing Castle Validation source code :
The default validation is PrototypeWebValidator, and this validator uses PrototypeValidationGenerator for client side validation, and this implementation doesn't do anything when you call "SetValueRange" :
so that seems logical that my client side validation is not working (the PrototypeValidationGenerator didn't implement this functionnality mainly because the underlying validation API, https://github.com/atetlaw/Really-Easy-Field-Validation didn't impement it as well).
So I decided to extend this because it's a framework and it's the purpose of a framework : provide a frame and let you work in it.
So I created the generator
The validator provide some functionnality for adding custom validation
The Validator that is needed by the formhelper :
And you wire castle to your validator on your base controller like this :
I have a BaseController but this solution is not the best, but I couldn't find out how to inject it in the castle monorail's configuration.
I'll try to commit this to the source base of Castle Monorail ...