我是用城堡鉴定的。
[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,但我可以看到我的法典与他之间的任何区别。