2012-01-13 93 views
1

我使用的是城堡的验证和我想知道为什么我的验证器不工作:如何在客户端为Castle验证创建自定义验证器?

[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,但我看不到我的代码和他的代码有什么区别。

回答

1

我终于找到了浏览城堡验证源代码后:

默认验证PrototypeWebValidator,这个验证程序使用PrototypeValidationGenerator用于客户端验证,当你调用“SetValueRange”此实现不执行任何操作:

public void SetValueRange(string target, int minValue, int maxValue, string violationMessage) 
     { 
     } 

,这样似乎是合乎逻辑,我的客户端验证不工作(在PrototypeValidationGenerator没有实现这个functionnality主要是因为基本验证API,https://github.com/atetlaw/Really-Easy-Field-Validation没有impement它)。

所以我决定扩展它,因为它是一个框架,它是一个框架的目的:提供一个框架,让你在其中工作。

因此,我创建了发电机

public class PrototypeValidationGeneratorExtension : PrototypeWebValidator.PrototypeValidationGenerator 
{ 
    private PrototypeWebValidator.PrototypeValidationConfiguration _config; 
    public PrototypeValidationGeneratorExtension(PrototypeWebValidator.PrototypeValidationConfiguration config, InputElementType inputType, IDictionary attributes) 
     :base(config,inputType,attributes) 
    { 
     _config = config; 
    } 
    public new void SetValueRange(string target, int minValue, int maxValue, string violationMessage) 
    { 
        this._config.AddCustomRule("validate-range",violationMessage,string.Format("min : {0}, max : {1}", minValue, maxValue)); 
    } 
} 

验证器添加自定义验证 由该表单助手所需的验证提供了一些functionnality:

public class PrototypeWebValidatorExtension : PrototypeWebValidator 
{ 
    public new IBrowserValidationGenerator CreateGenerator(BrowserValidationConfiguration config, InputElementType inputType, IDictionary attributes) 
    { 
     return new PrototypeValidationGeneratorExtension((PrototypeWebValidator.PrototypeValidationConfiguration)config, inputType, attributes); 
    } 
} 

你丝城堡在您的验证您的基本控制器是这样的:

 protected override void Initialize() 
     { 
      ((FormHelper)this.Helpers["Form"]).UseWebValidatorProvider(new PrototypeWebValidatorExtension()); 
// ... 
    } 

我有一个BaseController,但这个解决方案并不是最好的,但我无法找到如何将它注入城堡单轨的配置中。

我会尝试将其提交给Castle Monorail的源代码...