自定义StringByteLength Attribute

using HKElectric.ESafety.Utilities;
using System.ComponentModel.DataAnnotations;

namespace Test {
    public class StringByteLengthAttribute : ValidationAttribute {
        public int MaximumLength { get; set; }

        public StringByteLengthAttribute(int maximumLength) {
            MaximumLength = maximumLength;
        }

        public override string FormatErrorMessage(string name) {
            return string.Format(CustomeMessage.SAFETYDOCUMENT_BYTE_VERIFY, name, MaximumLength);
        }

        public override bool IsValid(object? value) {
            string? context = value != null ? value.ToString() : string.Empty;
            return !string.IsNullOrWhiteSpace(context) ? StringUtil.CountByteLength(context, MaximumLength) : true;
        }
    }
}

 

posted @ 2022-11-18 10:51  Tammytan  阅读(23)  评论(0编辑  收藏  举报