how to set a value of an input field using values in a repeater

I have this input fields inside a repeater, product name, price and quantity, and I have an input field total disabled by default outside of the repeater, I want to populate the result of foreach(price * quantity) inside the total field
Solution:
myabe you can add the afterStateUpdated directly to the repeater instead of the input fields. Try using ->afterStateUpdated(fn ($state) => dd($state)) on the repeater and examine the results. It should be an array with all the items in the repeater. This will fire when you click the add action of the repeater and when you update items in the repeater. New empty items have a uuid key. But I imagine you can easily traverse the array, make calculatioins and set the value to the total. Let me kn...
Jump to solution
9 Replies
Obala
ObalaOP16mo ago
my codes look like this
Repeater::make('items')
->relationship()
->schema([
Select::make('shop_product_id')
->options(Product::query()->pluck('name', 'id'))
->label('Stock')
->native(false)
->afterStateUpdated(fn($state, Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->placeholder('Select a drink'),
TextInput::make('quantity')
->required()
->numeric(),
TextInput::make('unit_price')
->label('Unit Price')
->disabled()dfd
->dehydrated()
->numeric()
->required()
->columnSpan([
'md' => 3,
]),
])->orderable()
->defaultItems(1)
->required(),

TextInput::make('total')
->disabled()
->required()
->numeric(),
]);
Repeater::make('items')
->relationship()
->schema([
Select::make('shop_product_id')
->options(Product::query()->pluck('name', 'id'))
->label('Stock')
->native(false)
->afterStateUpdated(fn($state, Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->placeholder('Select a drink'),
TextInput::make('quantity')
->required()
->numeric(),
TextInput::make('unit_price')
->label('Unit Price')
->disabled()dfd
->dehydrated()
->numeric()
->required()
->columnSpan([
'md' => 3,
]),
])->orderable()
->defaultItems(1)
->required(),

TextInput::make('total')
->disabled()
->required()
->numeric(),
]);
Obala
ObalaOP16mo ago
i tried implemting like this from an idea got from this discussion https://github.com/filamentphp/filament/discussions/5351
return $form
->schema([
Repeater::make('items')
->relationship()
->schema([
Select::make('shop_product_id')
->options(Product::query()->pluck('name', 'id'))
->label('Stock')
->native(false)
->afterStateUpdated(fn($state, Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->placeholder('Select a drink'),
TextInput::make('quantity')
->required()
->afterStateUpdated(fn ($state, Set $set, Get $get) => $set('../../total', Product::find($get('shop_product_id')?->price * $state)?? 0))
->numeric(),
TextInput::make('unit_price')
->label('Unit Price')
->disabled()
->dehydrated()
->numeric()
->required()
->columnSpan([
'md' => 3,
]),
])->orderable()
->defaultItems(1)
->required(),

TextInput::make('total')
->disabled()
->required()
->numeric(),
]);
return $form
->schema([
Repeater::make('items')
->relationship()
->schema([
Select::make('shop_product_id')
->options(Product::query()->pluck('name', 'id'))
->label('Stock')
->native(false)
->afterStateUpdated(fn($state, Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->placeholder('Select a drink'),
TextInput::make('quantity')
->required()
->afterStateUpdated(fn ($state, Set $set, Get $get) => $set('../../total', Product::find($get('shop_product_id')?->price * $state)?? 0))
->numeric(),
TextInput::make('unit_price')
->label('Unit Price')
->disabled()
->dehydrated()
->numeric()
->required()
->columnSpan([
'md' => 3,
]),
])->orderable()
->defaultItems(1)
->required(),

TextInput::make('total')
->disabled()
->required()
->numeric(),
]);
but its still not working, using ../.. is in v2 i don't if it can still be used in v3
GitHub
Set value of a field that is outside the repeater field, from a fie...
I'm trying to update the value of a field that is outside a repeater from a field that is inside the repeater, but the calculated value is doubling. For example, if I type 2 in the quantity fie...
Obala
ObalaOP16mo ago
it works with this but the problem when more that a single product is order for
TextInput::make('quantity')
->required()
->afterStateUpdated(fn ($state, Get $get, Set $set) => empty($get('../../total')) ? $set('../../total', $get('unit_price') * $state) : ($get('unit_price') * $state) + $get('../../total'))
->numeric(),
TextInput::make('quantity')
->required()
->afterStateUpdated(fn ($state, Get $get, Set $set) => empty($get('../../total')) ? $set('../../total', $get('unit_price') * $state) : ($get('unit_price') * $state) + $get('../../total'))
->numeric(),
tuto1902
tuto190216mo ago
Interesting 🧐 I’ve never used the ../ notation before. I’ll do some testing myself and let you know
Obala
ObalaOP16mo ago
Ok thanks, i await ur findings
Solution
tuto1902
tuto190216mo ago
myabe you can add the afterStateUpdated directly to the repeater instead of the input fields. Try using ->afterStateUpdated(fn ($state) => dd($state)) on the repeater and examine the results. It should be an array with all the items in the repeater. This will fire when you click the add action of the repeater and when you update items in the repeater. New empty items have a uuid key. But I imagine you can easily traverse the array, make calculatioins and set the value to the total. Let me know if that works
Obala
ObalaOP16mo ago
Let me test it out
tuto1902
tuto190215mo ago
@Obala if you don't mind, I'm marking this as solved since its been a few weeks since the last update
Obala
ObalaOP15mo ago
Oh yah totally forgot I have another though in Laravel , hope you can take a look at it, on middleware
Want results from more Discord servers?
Add your server