F
Filament4w 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
dyo4w ago
Can someone help me?
LeandroFerreira
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
dyo3w 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
toeknee3w ago
Change placeholder to default. else change placeholder to fromatStateUsing and condition if value is empty run function.
dyo
dyo3w 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
toeknee3w 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
dyo3w 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
toeknee3w ago
or use an observer.
dyo
dyo3w ago
sorry, I don't get what you mean about "set the placeholder content and make it reactive" and "use an observer"
toeknee
toeknee3w 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
toeknee3w 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
dyo3w 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
More Posts