1

城の検証を使用していますが、検証ツールが機能しない理由を知りたいです:

[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 と Implementing Castle.Components.Validator.IValidatorを見てきましたが、私のコードと彼のコードの間に違いは見られません。

4

1 に答える 1

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

バリデーターは、カスタム検証を追加するためのいくつかの機能を提供します。フォームヘルパーが必要とするバリデーター:

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

これをキャッスルモノレールのソースベースにコミットしようと思います...

于 2012-01-20T16:15:54.450 に答える