使用数据注释在 Winform 中进行验证

Validation in Winform using Data Annotations

我像这样创建一个验证 class:

public interface IDataErrorInfo
{
    string this[string columnName] { get; }
    string Error { get; }
}

public class BaseModel : IDataErrorInfo
{
   [Browsable(false)]
   public string this[string property]
    {
        get
        {
            var propertyDescriptor = TypeDescriptor.GetProperties(this)[property];
            if (propertyDescriptor == null)
                return string.Empty;

            var results = new List<ValidationResult>();
            var result = Validator.TryValidateProperty(
                                      propertyDescriptor.GetValue(this),
                                      new ValidationContext(this, null, null)
                                      { MemberName = property },
                                      results);
            if (!result)
                return results.First().ErrorMessage;
            return string.Empty;
        }
    }

    [Browsable(false)]
    public string Error
    {
        get
        {
            var results = new List<ValidationResult>();
            var result = Validator.TryValidateObject(this,
                new ValidationContext(this, null, null), results, true);
            if (!result)
                return string.Join("\n", results.Select(x => x.ErrorMessage));
            else
                return null;
        }
    }
}

然后我创建了一个 class 像这样的数据注释

public class ReaderSetting : BaseModel
{
    [JsonPropertyName("RfidAddress")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "RFID Address is required.")]
    [StringLength(3, ErrorMessage = "Max Length is 3")]
    public string RfidAddress { get; set; }
}

然后我这样试了一下:

private void btnAddSetting_Click(object sender, EventArgs e)
{
    var readerSetting = new ReaderSetting();
    try
    {
        readerSetting.RfidAddress = txtRfidAddress.Text; // txtRfidAddress.Text is blank
    }
    catch (ValidationException ex)
    {
        Console.WriteLine(ex.Message);
    }
    _readerSettingList.Add(readerSetting);
    var source = new BindingSource();
    source.DataSource = _readerSettingList;
    dgRfidSettings.DataSource = source;
}

怎么没通过ValidationException?

看看以下内容是否适合您。

.NET Fiddle 上试用。

public class ReaderSetting
{
    [JsonPropertyName("RfidAddress")]
    [Required(AllowEmptyStrings = false, ErrorMessage = "RFID Address is required.")]
    [StringLength(3, ErrorMessage = "Max Length is 3")]
    public string RfidAddress { get; set; }
}

public class EntityValidationResult
{
    public IList<ValidationResult> Errors { get; set; }
    public bool IsValid => Errors.Count == 0;
    public EntityValidationResult(IList<ValidationResult> errors = null)
    {
        Errors = errors ?? new List<ValidationResult>();
    }
}
public class EntityValidator<T> where T : class
{
    public EntityValidationResult Validate(T entity)
    {
        var validationResults = new List<ValidationResult>();
        var vc = new ValidationContext(entity, null, null);
        Validator.TryValidateObject(entity, vc, validationResults, true);

        return new EntityValidationResult(validationResults);
    }
}

用法

ReaderSetting readerSetting = new() { RfidAddress = "abcd" };
EntityValidator<ReaderSetting> validator = new EntityValidator<ReaderSetting>();
EntityValidationResult result = validator.Validate(readerSetting);
if (result.IsValid)
{
    Console.WriteLine("Valid");
}
else
{
    result.Errors.ToList().ForEach(x => Console.WriteLine(x.ErrorMessage));
}