F
Filamentβ€’4mo ago
Adysone

Update section heading programmatically

Hello, Is it possible to update a section heading programmatically? Or display a form field in it? I would like to display a "total cost" calculated with other fields of the form, in the heading of a section. The issue is that I put a value in the heading like so:
Forms\Components\Section::make()
->heading(fn(Get $get): string => (string) $get('total_cost'))
Forms\Components\Section::make()
->heading(fn(Get $get): string => (string) $get('total_cost'))
But my "total_cost" field is not defined on a creation form, because it's calculated after some fields are updated. How can I force the update of the heading? IS there a "boot" method on form builder?
Solution:
I finally did what I wanted by calling my "calculateTotalCost" in the heading() closure πŸ‘
Jump to solution
9 Replies
awcodes
awcodesβ€’4mo ago
->heading(function (Get $get, string $operation) {
if ($operation === 'edit') {
return $get('total_cost');
}

return 'Default Section heading';
})
->heading(function (Get $get, string $operation) {
if ($operation === 'edit') {
return $get('total_cost');
}

return 'Default Section heading';
})
Adysone
AdysoneOPβ€’4mo ago
I wish the 'Default Section heading' was $get('total_cost'), even at the creation. By default, the total_cost is calculated by doing some additions of other fields or settings
dissto
disstoβ€’4mo ago
I mean you probably just need to add live on the section πŸ€”
Section::make()
->live() // <--
->heading(fn(Get $get): string => (string) $get('total_cost'))
Section::make()
->live() // <--
->heading(fn(Get $get): string => (string) $get('total_cost'))
Should do it?
Adysone
AdysoneOPβ€’4mo ago
I tested but unfortunately it doesn't work
dissto
disstoβ€’4mo ago
Mhh i suppose you need to share more code then, because I just tested it, and it does work:
Section::make()
->live()
->heading(fn (Get $get) => $get('name') ?: 'default heading...')
Section::make()
->live()
->heading(fn (Get $get) => $get('name') ?: 'default heading...')
πŸ€”
Adysone
AdysoneOPβ€’4mo ago
Yes, I will share more code, I'm simplifiying it right now
Adysone
AdysoneOPβ€’4mo ago
My message is tool long so I joined my code in the message.txt file
Adysone
AdysoneOPβ€’4mo ago
If I intervert the group and the section, it works as I want... I do not really understand
Solution
Adysone
Adysoneβ€’4mo ago
I finally did what I wanted by calling my "calculateTotalCost" in the heading() closure πŸ‘
Want results from more Discord servers?
Add your server