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

Filament 3数值TextInput字段截断大数问题求助

解决Filament 3数值TextInput自动截断大数的问题

问题根源

你遇到的输入1000自动变为10的问题,核心原因是numeric mask的默认缩放行为:当你为quantity_in_kilos配置了decimalPlaces(2)的mask时,Filament默认会开启normalize模式——它会将输入的数字字符串按小数位数自动缩放(比如输入1000会被解析为10.00,即1000 / 10^2),结合你添加的公斤转克逻辑,进一步放大了这个错误转换。

解决方案

方案1:关闭mask的自动缩放

修改quantity_in_kilos的mask配置,添加->normalize(false),让输入的数字保持原样解析:

Forms\Components\TextInput::make('quantity_in_kilos')
    ->numeric()
    ->required()
    ->label('Quantity (kilos)')
    ->reactive()
    ->mask(fn (Forms\Components\TextInput\Mask $mask) => $mask
        ->numeric()
        ->decimalPlaces(2)
        ->decimalSeparator('.')
        ->thousandsSeparator(',')
        ->minValue(0)
        ->normalize(false) // 关键:关闭自动缩放逻辑
    )
    ->afterStateUpdated(function (Get $get, Set $set, $state) {
        // 公斤转克逻辑保持不变
        $grams = $state * 1000;
        $set('quantity_in_grams', $grams);
    })

方案2:移除mask,改用原生numeric配置

如果不需要千分位分隔符,可直接使用numeric组件的原生精度配置,避免mask的自动转换:

Forms\Components\TextInput::make('quantity_in_kilos')
    ->numeric()
    ->required()
    ->label('Quantity (kilos)')
    ->reactive()
    ->decimalPlaces(2)
    ->minValue(0)
    ->maxValue(999999.99)
    ->afterStateUpdated(function (Get $get, Set $set, $state) {
        $grams = $state * 1000;
        $set('quantity_in_grams', $grams);
    })

额外检查项

  1. 数据库字段类型验证:确保quantity_in_grams的字段类型足够容纳转换后的值,建议使用decimal(10,2)(支持最大99999999.99)或bigint(若存储整数克数),避免数值溢出。
  2. Repeater状态路径确认:Repeater内的$set('quantity_in_grams', $grams)无需额外路径前缀,每个Repeater项的表单上下文是独立的,该写法是正确的。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 01:29:51