F
Filament7mo ago
dyo

Suggestion for dependant fields

Can someone suggest me what is the best approach for dependant fields.. In my case, I want my input total_price to be dependant to: 1. room_id, which is later will have to get a price attribute from room model, required 2. discount, numeric input, not required 3. additional_services (with repeater), has name and price attribute, can be 0 items or not required. So later, the input price will be a calculated data of those above 3 inputs. Currently the form is in resource class.. I'm using filament v2.
Solution:
thanks for your response before.. I've decided to calculate what's in repeater in afterCreate method.....
Jump to solution
13 Replies
dyo
dyoOP7mo ago
Can someone help me?
LeandroFerreira
LeandroFerreira7mo ago
You could use Total Price as a placeholder and calculate it using the values from other fields. If you’d like a suggestion, please share some of the code you’re using.
dyo
dyoOP7mo ago
TextInput::make('total_price')
->placeholder(function (Closure $get) {
$value = 0;
$value += Room::find($get('room_id'))?->price;

if ($get('discount')) {
$value -= $get('discount');
}

foreach ($get('additional_services') as $key => $row) {
$value += $row['price'];
}

return number_format($value);
})
->disabled(),
TextInput::make('total_price')
->placeholder(function (Closure $get) {
$value = 0;
$value += Room::find($get('room_id'))?->price;

if ($get('discount')) {
$value -= $get('discount');
}

foreach ($get('additional_services') as $key => $row) {
$value += $row['price'];
}

return number_format($value);
})
->disabled(),
How can I submit the form with what is defined in total_price placeholder?
toeknee
toeknee7mo ago
Change placeholder to default. else change placeholder to fromatStateUsing and condition if value is empty run function.
dyo
dyoOP7mo ago
i've tried using default and fromatStateUsing with the same function as placeholder above, but the value isn't updated after changing or filling all of 3 other inputs, still 0
toeknee
toeknee7mo ago
Ahh it's disabled, PI missed that. why disable it if you want to save it? use mutateBeforeSave if you want it to set what it will be
dyo
dyoOP7mo ago
yup, I don't want the user to edit the total price directly.. i'm still in filament v2.. did you mean mutateFormDataBeforeCreate? how can I calculate my repeater data there?
toeknee
toeknee7mo ago
toeknee
toeknee7mo ago
or use an observer.
dyo
dyoOP7mo ago
sorry, I don't get what you mean about "set the placeholder content and make it reactive" and "use an observer"
toeknee
toeknee7mo ago
Placeholder::make('total_price')
->content(function (Closure $get) {
$value = 0;
$value += Room::find($get('room_id'))?->price;

if ($get('discount')) {
$value -= $get('discount');
}

foreach ($get('additional_services') as $key => $row) {
$value += $row['price'];
}

return number_format($value);
}),
Placeholder::make('total_price')
->content(function (Closure $get) {
$value = 0;
$value += Room::find($get('room_id'))?->price;

if ($get('discount')) {
$value -= $get('discount');
}

foreach ($get('additional_services') as $key => $row) {
$value += $row['price'];
}

return number_format($value);
}),
Set discount and addditional_Services to 'reactive()'
toeknee
toeknee7mo ago
Then on an Oberse or in use a place holder, set the placeholder content and make it reactive. It depends but: https://filamentphp.com/docs/2.x/admin/resources/creating-records#customizing-data-before-saving do the same calcs.
Solution
dyo
dyo7mo ago
thanks for your response before.. I've decided to calculate what's in repeater in afterCreate method..
Want results from more Discord servers?
Add your server