Number decimal formatting is not working
->schema([
Forms\Components\Repeater::make('items')
->relationship()
->schema([
Forms\Components\Select::make('expense_category_id')
->label('Category')
->options(
ExpenseCategory::query()
->pluck('name', 'id')
)
->required()
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->searchable(),
Forms\Components\TextInput::make('unit_price')
->label('Unit Price')
->mask(RawJs::make('$money($input)'))
->stripCharacters(',')
->numeric()
->inputMode('decimal')
->required()
])
->live(debounce: 500)
->afterStateUpdated(
fn(Forms\Set $set, Forms\Get $get) =>
$set(
'total_amount',
round(collect($get('items'))
->sum(
fn($item)
=> (float) str_replace(',', '', $item['unit_price'] ?? 0)
), 2)
)
)
Forms\Components\TextInput::make('total_amount')
->label('Total')
->mask(RawJs::make('$money($input)'))
->numeric()
->reactive()
->inputMode('decimal')
->readOnly()
->required()
->dehydrateStateUsing(
fn($state) => (float) str_replace(',', '', $state)
)
->schema([
Forms\Components\Repeater::make('items')
->relationship()
->schema([
Forms\Components\Select::make('expense_category_id')
->label('Category')
->options(
ExpenseCategory::query()
->pluck('name', 'id')
)
->required()
->distinct()
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->searchable(),
Forms\Components\TextInput::make('unit_price')
->label('Unit Price')
->mask(RawJs::make('$money($input)'))
->stripCharacters(',')
->numeric()
->inputMode('decimal')
->required()
])
->live(debounce: 500)
->afterStateUpdated(
fn(Forms\Set $set, Forms\Get $get) =>
$set(
'total_amount',
round(collect($get('items'))
->sum(
fn($item)
=> (float) str_replace(',', '', $item['unit_price'] ?? 0)
), 2)
)
)
Forms\Components\TextInput::make('total_amount')
->label('Total')
->mask(RawJs::make('$money($input)'))
->numeric()
->reactive()
->inputMode('decimal')
->readOnly()
->required()
->dehydrateStateUsing(
fn($state) => (float) str_replace(',', '', $state)
)

4 Replies
It does work fine, the issue is your expecting it to add decimals, but you need to add them if you want to do it automatically.
i.e. with
->formatStateUsing(fn ($state) => number_format((float) $state, 2))
and afterStateUpdated too
let me check. Thank you
Not working
What have you done
here is my update code
Forms\Components\Section::make('Expense Details')
/// other data
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->searchable(),
Forms\Components\TextInput::make('unit_price')
->label('Unit Price')
->mask(RawJs::make('$money($input)'))
->stripCharacters(',')
->numeric()
->inputMode('decimal')
->required()
])
->live(debounce: 600)
->afterStateUpdated(
fn(Forms\Set $set, Forms\Get $get) =>
$set(
'total_amount',
round(collect($get('items'))
->sum(
fn($item) => (float) str_replace(
',',
'',
$item['unit_price'] ?? 0
)
), 2)
)
)
->hiddenLabel()
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('total_amount')
->label('Total')
->numeric()
->reactive()
->inputMode('decimal')
->readOnly()
->afterStateUpdated(
fn($state, Forms\Set $set) => $set('total_amount', number_format((float) $state, 2))
)
->required()
])->columnSpan(1)
])->columns(3),
Forms\Components\Section::make('Expense Details')
/// other data
->disableOptionsWhenSelectedInSiblingRepeaterItems()
->searchable(),
Forms\Components\TextInput::make('unit_price')
->label('Unit Price')
->mask(RawJs::make('$money($input)'))
->stripCharacters(',')
->numeric()
->inputMode('decimal')
->required()
])
->live(debounce: 600)
->afterStateUpdated(
fn(Forms\Set $set, Forms\Get $get) =>
$set(
'total_amount',
round(collect($get('items'))
->sum(
fn($item) => (float) str_replace(
',',
'',
$item['unit_price'] ?? 0
)
), 2)
)
)
->hiddenLabel()
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('total_amount')
->label('Total')
->numeric()
->reactive()
->inputMode('decimal')
->readOnly()
->afterStateUpdated(
fn($state, Forms\Set $set) => $set('total_amount', number_format((float) $state, 2))
)
->required()
])->columnSpan(1)
])->columns(3),