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