C# Model 自定义检验

使Model实现IValidatableObject接口并重写Validate方法即可

 public class AuditIPKeepRecordApply_In: IValidatableObject
    {
        /// <summary>
        /// IP备案申请表主键id
        /// </summary>
        [Required(ErrorMessage = "IP备案申请表主键id不可为空")]
        public string Id { get; set; }
        
        /// <summary>
        /// 审核反馈
        /// </summary>
        public string? Feedback { get; set; }
        
        /// <summary>
        /// 状态
        /// </summary>
        [Required(ErrorMessage = "状态不可为空")]
        [EnumDataType(typeof(EnumRecordState), ErrorMessage = "状态类型错误")]
        public EnumRecordState State { get; set; }
        
        /// <summary>
        /// 有效截止日
        /// </summary>
        public DateTime? ExpirationDate { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (State == EnumRecordState.Pass && ExpirationDate == null)
            {
                yield return new ValidationResult(
                    "当审核状态为通过时有效截止日不可为空!",
                    new[] { "AuditIPKeepRecordApply_In" }
                );
            }
        }
posted @ 2023-10-08 11:14  SpringCore  阅读(15)  评论(0编辑  收藏  举报