如何在客户端为Castle验证构建自定义Validator?

时间:2012-01-13 16:24:09

标签: c# castle castle-validators

我正在使用城堡验证,我想知道为什么我的验证器不起作用:

[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 个答案:

答案 0 :(得分:1)

我在浏览Castle Validation源代码后终于找到了它:

默认验证是PrototypeWebValidator,此验证器使用PrototypeValidationGenerator进行客户端验证,当您调用“SetValueRange”时,此实现不执行任何操作:

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

所以我的客户端验证不起作用似乎是合乎逻辑的(PrototypeValidationGenerator没有实现这个功能,主要是因为底层验证API,https://github.com/atetlaw/Really-Easy-Field-Validation也没有实现它。)

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

所以我创建了生成器

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));
    }
}

验证器为添加自定义验证提供了一些功能 formhelper需要的Validator:

 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的源基地......