使用带有复杂类型模型的 ObjectGraphDataAnnotationsValidator 进行业务验证

问题描述

在我的 blazor 服务器应用程序中,我根据 this

进行了业务验证
   @page "/"
   @inherits IndexBase
   <EditForm Model="@MyModel" OnValidSubmit="@HandleValidSubmit">
    <ObjectGraphDataAnnotationsValidator />
    <CustomValidator @ref="customValidator" />
    <ValidationSummary />
      <div>
        <InputText @bind-Value="MyModel.Customer.Name"></InputText>
        <ValidationMessage For="()=>MyModel.Customer.Name" />
      </div>
      <div>
        <InputNumber @bind-Value="MyModel.Customer.Age"> </InputNumber>
        <ValidationMessage For="()=>MyModel.Customer.Age" />
      </div>
      <button Text="Save" type="submit">Get Result</button>
    </EditForm>

及其基类包含

    public class IndexBase:ComponentBase
     {
       protected MyModel MyModel { get; set; } = new MyModel();
       protected CustomValidator customValidator;
       protected void HandleValidSubmit()
        {
        customValidator.ClearErrors();
        var errors = new Dictionary<string,List<string>>();
        if ( MyModel.Customer.Name.ToLower().StartsWith("a"))
        {
            errors.Add("Customer.Age",new List<string>() { "Age should be greater than 10" });
        }
        if (errors.Count() > 0)
        {
            customValidator.displayErrors(errors);
        }
        else
        {
            // Process the form
        }
    }
}

和 MyModel 看起来像

     public class MyModel
      {
        [ValidateComplexType]
        public Customer Customer { get; set; } = new Customer();
      }

public class Customer
{
    [required]
    public string Name { get; set; }
    public int Age { get; set; }0
}

我的 CustomValidator 看起来像下面的代码(来自 here

         public class CustomValidator : ComponentBase
         {
          private ValidationMessageStore messageStore;

       [CascadingParameter]
       private EditContext CurrentEditContext { get; set; }

       protected override void OnInitialized()
       {
        if (CurrentEditContext == null)
        {
            throw new InvalidOperationException(
                $"{nameof(CustomValidator)} requires a cascading " +
                $"parameter of type {nameof(EditContext)}. " +
                $"For example,you can use {nameof(CustomValidator)} " +
                $"inside an {nameof(EditForm)}.");
        }

        messageStore = new ValidationMessageStore(CurrentEditContext);

        CurrentEditContext.OnValidationRequested += (s,e) =>
            messageStore.Clear();
        CurrentEditContext.OnFieldChanged += (s,e) =>
            messageStore.Clear(e.FieldIdentifier);
    }

    public void displayErrors(Dictionary<string,List<string>> errors)
    {
        foreach (var err in errors)
        {
            FieldIdentifier x = CurrentEditContext.Field(err.Key);
            messageStore.Add(x,err.Value);
        }

        CurrentEditContext.NotifyValidationStateChanged();
    }

    public void ClearErrors()
    {
        messageStore.Clear();
        CurrentEditContext.NotifyValidationStateChanged();
    }
}

当我点击提交按钮时,验证代码有效,但没有向输入控件添加修改无效类。所以这不显示错误信息和红色边框来控制

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)