You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor自定义验证错误重复显示问题求助

问题解决:Blazor自定义验证错误重复显示两次

问题背景

在Blazor项目中,原本自定义验证规则运行正常:勾选复选框时,二级字段变为必填项。添加复选框勾选显示二级字段、取消勾选隐藏的逻辑后,所有验证错误会重复显示两次,调试发现自定义验证方法会进入错误分支两次。

代码回顾

页面代码

<RadzenLabel Text="Other:  " />&nbsp;&nbsp; <RadzenCheckBox @bind-Value=@WIR40Entity.type_other  Name="CheckBox15" @onclick="@HideOtherP"  />

<RadzenLabel Text="If Other, Please Describe:  " hidden="@HideOther" />&nbsp;&nbsp;<RadzenTextBox hidden="@HideOther" @bind-Value=@WIR40Entity.OtherIncidentType onkeydown="return (event.keyCode!=13);" MaxLength="100" class="w-100" />

逻辑代码

private bool HideOther { get; set; } = true;

public void HideOtherP()
{
    HideOther = !HideOther;
}

模型与自定义验证

// 模型
[CustomCompareAttributesOther(nameof(OtherIncidentType))]
public bool? type_other { get; set; }

// 自定义验证特性
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    WIR40 entity = (WIR40)validationContext.ObjectInstance;
    if (entity.type_other == true && string.IsNullOrWhiteSpace(entity.OtherIncidentType))
    {
        return new ValidationResult("When selecting Other as an Incident Action: A Description is also required.");
    }
    return ValidationResult.Success;
}

原因分析

问题出在两次状态更新触发了两次验证:

  1. RadzenCheckBox的@bind-Value会在勾选/取消时自动更新WIR40Entity.type_other的值,触发一次组件状态变更和验证。
  2. 同时绑定的@onclick="@HideOtherP"会手动切换HideOther的值,再次触发组件重新渲染和验证。
    两次状态变更导致自定义验证方法被执行两次,错误信息也就重复显示了。

解决办法

方法1:合并状态更新逻辑,避免重复触发

取消@onclick绑定,改用@bind-Value:after事件,在type_other的值更新完成后,直接根据新值设置HideOther,确保只有一次状态变更:

修改后的页面代码

<RadzenLabel Text="Other:  " />&nbsp;&nbsp; 
<RadzenCheckBox @bind-Value=@WIR40Entity.type_other  
                Name="CheckBox15" 
                @bind-Value:after="UpdateOtherVisibility"  />

<RadzenLabel Text="If Other, Please Describe:  " hidden="@HideOther" />&nbsp;&nbsp;
<RadzenTextBox hidden="@HideOther" 
               @bind-Value=@WIR40Entity.OtherIncidentType 
               onkeydown="return (event.keyCode!=13);" 
               MaxLength="100" 
               class="w-100" />

修改后的逻辑代码

private bool HideOther { get; set; } = true;

public void UpdateOtherVisibility()
{
    // 直接根据type_other的当前值控制显示/隐藏,无需取反
    HideOther = WIR40Entity.type_other != true;
}

方法2:优化自定义验证,确保单次错误输出

如果还有重复验证的情况,可以在验证特性中指定错误关联的字段,避免全局重复显示:

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
    WIR40 entity = (WIR40)validationContext.ObjectInstance;
    if (entity.type_other == true && string.IsNullOrWhiteSpace(entity.OtherIncidentType))
    {
        return new ValidationResult(
            "When selecting Other as an Incident Action: A Description is also required.",
            new[] { nameof(WIR40.OtherIncidentType), nameof(WIR40.type_other) });
    }
    return ValidationResult.Success;
}

验证效果

修改后,勾选/取消复选框只会触发一次状态更新,自定义验证方法仅执行一次,错误信息不会重复显示,同时保留了字段的显示/隐藏逻辑和必填验证规则。

内容的提问来源于stack exchange,提问作者ExecChef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 10:10:35