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

ABP.IO为IdentityUser加RequiredAttribute被忽略是Blazor限制吗

问题原因及解决方法

  • 首先要确认你的ObjectExtension配置代码的摆放位置,必须放在对应模块ConfigureServices方法的最前端,早于ABP框架Identity模块的初始化逻辑,否则框架已经预加载完DTO元数据后,你新增的特性不会被识别。
  • 其次你缺少ABP Identity模块的内置配置项开启:ABP 5.0+默认允许Name、Surname字段为空,你需要额外开启必填的全局配置:
Configure<IdentityUserOptions>(options =>
{
    options.IsNameAndSurnameRequired = true;
});
  • 最后修改你的属性扩展代码,对于已有属性添加特性要先清理旧的同类型特性,同时补充Blazor端的验证识别配置:
ObjectExtensionManager.Instance
    .AddOrUpdateProperty<string>(
        new[]
        {
            typeof(IdentityUserDto),
            typeof(IdentityUserCreateDto),
            typeof(IdentityUserUpdateDto)
        },
        "Name",
        property =>
        {
            // 先移除已有RequiredAttribute避免冲突
            property.Attributes.RemoveAll(x => x is RequiredAttribute);
            property.Attributes.Add(new RequiredAttribute());
            // 补充Blazor表单的必填识别配置
            property.Configuration["Blazor.Validation.Required"] = true;
        }
    );

Surname属性的配置逻辑和上面完全一致,重复加一次配置即可。
如果以上配置都完成仍然不生效,是因为ABP Blazor UI的预定义用户表单字段优先级高于动态扩展配置,可以直接自定义用户创建/编辑页面的扩展点,手动给对应字段加必填验证,兼容性更高。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 20:36:09