【问题标题】:Weird error when using Fluent Validation API and Mud Blazor使用 Fluent Validation API 和 Mud Blazor 时出现奇怪的错误
【发布时间】:2022-08-18 23:39:38
【问题描述】:

对我们的某些模型的某些 NotEqual 规则使用 Fluent 验证,并在验证应该成功时得到一个奇怪的错误。我们的模型有几个字段都是复杂的用户模型。它们不能彼此相同。当它们相同时,流利的验证器可以正常工作并显示我的自定义错误消息。当它们不相同时,不应显示任何错误文本,这就是我所看到的:

真的没有意义。知道发生了什么吗?

验证器类:

using FluentValidation;
using Data.Models.DBModels;

namespace Compyl.WebApp.Validators
{
    public class AssessmentModelFluentValidator : AbstractValidator<AssessmentModel>
    {
        public AssessmentModelFluentValidator()
        {
            RuleLevelCascadeMode = CascadeMode.Stop;
           
            
            RuleFor(x => x.Owner)
           
                .NotEqual(x=> x.DelegateOwner)
                .WithMessage(\"Owner cannot be the same as Delegate Owner\")
                .NotEqual(X => X.Reviewer)
                .WithMessage(\"Owner cannot be the same as Reviewer\");

            RuleFor(x => x.DelegateOwner)       
                .NotEqual(x => x.Owner)
                .WithMessage(\"Delegate Owner cannot be the same as Owner\")
                .NotEqual(X => X.Reviewer)
                .WithMessage(\"Delegate Owner cannot be the same as Reviwer\");



            RuleFor(x => x.Reviewer)
      
                .NotEqual(x => x.Owner)
                .WithMessage(\"Reviewer cannot be the same as Owner\")
                .NotEqual(X => X.DelegateOwner)
                .WithMessage(\"Reviewer cannot be the same as Delegate Owner\");


        }

        public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
        {
            var result = await ValidateAsync(ValidationContext<AssessmentModel>.CreateWithOptions((AssessmentModel)model, x => x.IncludeProperties(propertyName)));
            if (result.IsValid)
                return Array.Empty<string>();
            return result.Errors.Select(e => e.ErrorMessage);
        };
    }
}

表格的一部分:


<MudForm  @ref=\"form\" Model=\"@assessment\" @bind-IsValid=\"@success\"  Validation=\"@(assessmentValidator.ValidateValue)\" @bind-Errors=\"@errors\">
  <MudCard Elevation=\"0\">
     <MudCardHeader>
         <CardHeaderContent>
             <MudText Typo=\"Typo.h6\">@assessment.Name</MudText>
              <MudText>@assessment.Description</MudText>
          </CardHeaderContent>
                    
       </MudCardHeader>
       <MudCardContent>
        <MudTabs Elevation=\"2\" Rounded=\"true\" ApplyEffectsToContainer=\"true\" PanelClass=\"pa-6\">
            <MudTabPanel Text=\"Assessment Details\">
                  <MudTextField  Immediate=\"true\" For=\"@(() => assessment.Name)\"  Variant=\"Variant.Outlined\" Disabled=\"@(!canEdit)\" RequiredError=\"Assessment Name is Required!\"  Required Label=\"Name\" @bind-Value=\"assessment.Name\" />
                   <MudTextField  For=\"@(() => assessment.Description)\"  Variant=\"Variant.Outlined\"  Disabled=\"@(!canEdit)\" Lines=\"4\" Label=\"Description\" @bind-Value=\"assessment.Description\" />
                   <MudAutocomplete   Immediate=\"true\" For=\"@(() => assessment.Owner)\" Variant=\"Variant.Outlined\"  Disabled=\"@(!canEdit)\" T=\"UserModel\"  @bind-Value=\"assessment.Owner\" RequiredError=\"Assessment Owner is Required!\" Required Label=\"Owner\"   ToStringFunc=\"@(e=> e==null? null : $\"{e.Email}\")\" SearchFunc=\"@SearchUsers\" />
                 <MudAutocomplete  Immediate=\"true\" For=\"@(() => assessment.DelegateOwner)\" Variant=\"Variant.Outlined\" Disabled=\"@(!canEdit)\" T=\"UserModel\"  @bind-Value=\"assessment.DelegateOwner\" Label=\"Delegate Owner\" ToStringFunc=\"@(e=> e==null? null : $\"{e.Email}\")\" SearchFunc=\"@SearchUsers\" />
                  <MudAutocomplete  Immediate=\"true\" For=\"@(() => assessment.Reviewer)\" Variant=\"Variant.Outlined\" Disabled=\"@(!canEdit)\" T=\"UserModel\" @bind-Value=\"assessment.Reviewer\" RequiredError=\"Assessment Reviewer is Required!\" Required Label=\"Reviewer\"  ToStringFunc=\"@(e=> e==null? null : $\"{e.Email}\")\" SearchFunc=\"@SearchUsers\" />

    标签: c# asp.net-core blazor fluentvalidation mudblazor


    【解决方案1】:

    您无法使用 Fluent Validation 比较对象。您必须比较各个属性。在这种情况下,比较一个 ID 是有意义的(我猜是属性名称)

    RuleFor(x => x.Reviewer.Id)
        .NotEqual(x => x.Owner.Id)
        .WithMessage("Reviewer cannot be the same as Owner")
        .NotEqual(X => X.DelegateOwner.Id)
        .WithMessage("Reviewer cannot be the same as Delegate Owner");
    

    【讨论】:

    • 我已经尝试过了,并使用了它们的一个属性。同样的错误。
    • 我认为这与您的 ValidateAsync() 调用有关,但如果没有更多上下文/调用代码,我无法确定。也许您应该尝试将它正在验证的属性限制为仅相关的属性名称。检查有关此的文档。
    【解决方案2】:

    这部分是 Bron 在评论中的回答,但 MudBlazor 的“For”也不能处理复杂的对象。解决方法是将复杂模型的 ID 绑定到字段,而不是完整模型。这样,您可以改为为 ID 设置规则和 for 语句。

    【讨论】:

      猜你喜欢
      • 2023-02-22
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 2020-08-30
      • 2013-05-05
      • 2014-09-24
      • 1970-01-01
      • 2012-05-05
      相关资源
      最近更新 更多