繁体   English   中英

如何在客户端为城堡验证构建自定义验证器?

[英]How to build a custom Validator for Castle Validation on client-side?

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

[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无效。

我已经看到了这个线程的最小长度自定义AbstractValidationAttribute和实现Castle.Components.Validator.IValidator ,但是我看不到我的代码和他的代码之间的任何区别。

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

默认验证是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所需的验证器:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM