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
Obala11mo 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
Obala11mo 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
Obala11mo 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
tuto190211mo ago
Interesting 🧐 I’ve never used the ../ notation before. I’ll do some testing myself and let you know
Obala
Obala11mo ago
Ok thanks, i await ur findings
Solution
tuto1902
tuto190211mo 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
Obala11mo ago
Let me test it out
tuto1902
tuto190210mo ago
@Obala if you don't mind, I'm marking this as solved since its been a few weeks since the last update
Obala
Obala10mo 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
More Posts
Illuminate/Support not loaded while trying to install nunomaduro/larastanHello there friends. As part of following the Pet Clinic project and learn the filament admin insidWorking on a Laravel Filament project using remote Shared HostingHello guys. As part of my "training punishment" I decided to try to install a fresh Laravel + Filamis there a way to mutate data on createOptionForm in Select input.i want to attach the creator_id before saving the form instead of having the a readonly field. ` Fis there a way to make tabs filter optional according to rolesi have a teacher role and admin role, i want a tab filter only when i login as an admin, if i login Render data from two resourcesIs there a way to render data in a form from two different resources (tables or models) without a reNeed Help wrapping my mind around Filament implementationHello! I recently stumbled across this channel and love the content. Thank you for providing such grhow to share resources on both panelsi have 2 panel, teacher and stundet, i want to create a resource that accessible for both roles, thnCustom setting page with filamentphpI'm attempting to set up the edit page as the primary view for a filamentphp resource, specifically Validation doesn't work on form created with createOptionFormFolks, I used the createOptionForm function to enable a new record creation form in a select field. Following this post here, since it's Filament related@shaan4237 https://discord.com/channels/1138703914523443310/1145911306092826725/1145911306092826725