LMS.service/LMS.Common/Attributes/NotEmptyAttribute.cs

18 lines
552 B
C#
Raw Normal View History

using System.ComponentModel.DataAnnotations;
namespace LMS.Common.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class NotEmptyAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value is string str && string.IsNullOrWhiteSpace(str))
{
return new ValidationResult(ErrorMessage ?? "字段不能为空");
}
return ValidationResult.Success;
}
}
}