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

The ValidateNonEmpty validate on both client and server side, but the ValidatePositiveInteger is not.

I ve see this thread Min Length Customs 摘要ValidationAttribute and Implementing 卡斯尔,Components.Validator.IValidator,但我可以看到我的法典与他之间的任何区别。

最佳回答

我最后在浏览《卡斯尔瓦底放射源法》之后发现:

缺省验证是PrototypeWebValidator,该有效器使用原型发电机进行客户对口验证,如果你称“SetValueRange”的话,这种执行就没有任何意义:

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

因此,似乎合乎逻辑的是,我的客户方验证工作不可行(PrototypeValidationGenerator don t 执行这一功能性,主要是因为基本验证结果,。 也没有削减。

因此,我决定扩大这一范围,因为它提供了一个框架,它符合框架的目的:提供一个框架,让你在其中工作。

因此,我建立了发电机

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

The validator provide some functionnality for adding custom validation The Validator that is needed by the 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());
//    ...
    }

我有一个基地主计长,但这一解决办法并非最佳办法,但我无法找到如何在城堡单轨组合中注入这一解决办法。

我试图将此事投入到Casa Monorail的基地......

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签