Castle Validators如何验证是否小于/大于或等于

时间:2009-08-31 15:34:11

标签: castle-validators

我看到了ValidateIsLesser和ValidateIsGreater属性。但是如果我想做一个ValidateIsLesserOrEqual和/或ValidateIsGreaterOrEqual怎么办?我只需要编写这些自定义属性,还是有预先构建的功能,我很难找到它?

[ValidateNonEmpty]
[ValidateDate]
[ValidateIsLesser(IsLesserValidationType.Date, "EndDate", "Start Date must be before End Date.")]
public DateTime StartDate { get; set; }

[ValidateNonEmpty]
[ValidateDate]
[ValidateIsGreater(IsGreaterValidationType.Date, "StartDate", "End Date must be after the Start Date.")]
public DateTime EndDate { get; set; }

1 个答案:

答案 0 :(得分:2)

您可以使用 ValidateSelf 属性并为OrEqual比较提供您自己的验证:

[ValidateNonEmpty]
[ValidateDate]
public DateTime StartDate { get; set; }

[ValidateNonEmpty]
[ValidateDate]
public DateTime EndDate { get; set; }

[ValidateSelf]
public void ValidateDate(ErrorSummary errors)
{
    if (StartDate >= EndDate)
        errors.RegisterErrorMessage("StartDate", "Start date must be earlier than end date.");
}