2012-01-13 12 views
1

私は城の検証を使用していると私は私のバリデータが動作しない理由を知りたいのですが:クライアント側で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を見ましたが、私は自分のコードと彼の間に違いは見られません。

答えて

1

私は最終的に城の検証のソースコードを閲覧した後にそれを見つけた:

デフォルトの検証はPrototypeWebValidatorであり、このバリデータは、クライアント側の検証のためのPrototypeValidationGeneratorを使用し、あなたが「SetValueRange」を呼び出すときに、この実装は何もしません。

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

ので、それは私のクライアント側の検証は(PrototypeValidationGeneratorがhttps://github.com/atetlaw/Really-Easy-Field-Validationは同様にそれをimpementなかった、主に基礎となる検証APIので、このfunctionnalityを実装していませんでした)動作していないことを論理的と思われます。

フレームワークであり、フレームワークの目的であるため、フレームを提供して作業してください。これを拡張することにしました。

は、だから私は、バリデータが FormHelperので必要とされるバリデータをカスタム検証を追加するためのいくつかのfunctionnalityを提供

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

ジェネレータを作成しました:

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を持っていますが、このソリューションは最高ではありませんが、私は城モノレールの構成に注入する方法を見つけることができませんでした。

私はキャッスルモノレールのソース基地にこれをコミットしようとします...