zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
commit | author | age
921de2 1 using Microsoft.AspNetCore.Components;
Z 2 using System;
3 using System.Collections.Generic;
4 using System.ComponentModel.DataAnnotations;
5
6 namespace Bootstrap.Admin.Pages.Components
7 {
8     /// <summary>
9     /// 验证组件基类
10     /// </summary>
11     public abstract class ValidatorComponentBase : ComponentBase
12     {
13         /// <summary>
14         /// 获得/设置 错误描述信息
15         /// </summary>
16         [Parameter]
17         public string ErrorMessage { get; set; } = "";
18
19         /// <summary>
20         /// 获得/设置 IRules 实例
21         /// </summary>
22         [CascadingParameter]
23         public IRules? Input { get; set; }
24
25         /// <summary>
26         /// 初始化方法
27         /// </summary>
28         protected override void OnInitialized()
29         {
30             if (Input == null)
31             {
32                 throw new InvalidOperationException($"{nameof(ValidatorComponentBase)} requires a cascading " +
33                     $"parameter of type {nameof(IRules)}. For example, you can use {nameof(ValidatorComponentBase)} " +
34                     $"inside an LgbInputText.");
35             }
36
37             Input.Rules.Add(this);
38         }
39
40         /// <summary>
41         /// 验证方法
42         /// </summary>
43         /// <param name="propertyValue"></param>
44         /// <param name="context"></param>
45         /// <param name="results"></param>
46         public abstract void Validate(object? propertyValue, ValidationContext context, List<ValidationResult> results);
47     }
48 }