zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
commit | author | age
921de2 1 using System.Collections.Generic;
Z 2 using System.ComponentModel.DataAnnotations;
3 using Microsoft.AspNetCore.Components;
4
5 namespace Bootstrap.Admin.Pages.Components
6 {
7     /// <summary>
8     /// 
9     /// </summary>
10     public class RequiredValidator : ValidatorComponentBase
11     {
12         /// <summary>
13         /// 
14         /// </summary>
15         public RequiredValidator()
16         {
17             ErrorMessage = "这是必填字段";
18         }
19
20         /// <summary>
21         /// 获得/设置 是否允许空字符串 默认 false 不允许
22         /// </summary>
23         [Parameter]
24         public bool AllowEmptyString { get; set; }
25
26         /// <summary>
27         /// 
28         /// </summary>
29         /// <param name="propertyValue"></param>
30         /// <param name="context"></param>
31         /// <param name="results"></param>
32         public override void Validate(object? propertyValue, ValidationContext context, List<ValidationResult> results)
33         {
34             var val = propertyValue?.ToString() ?? "";
35             if (!AllowEmptyString && val == string.Empty)
36             {
37                 results.Add(new ValidationResult(ErrorMessage, new string[] { context.MemberName }));
38             }
39         }
40     }
41 }