F
Filamentβ€’2mo ago
Ed

How to change a label for a field, in view page?

I have a Main Resource file, that defines the form like so (See attached image). I am needing to change the field label only when viewing a record, based on a if condition on what the value of this record is. For example: If value is FR then the label should be Company Registration France. How can I achieve so in the View Page file? <?php namespace App\Filament\Resources\AllClientsResource\Pages; use App\Filament\Resources\AllClientsResource; use Filament\Resources\Pages\ViewRecord; class ViewClient extends ViewRecord { protected static string $resource = AllClientsResource::class; protected function mutateFormDataBeforeFill(array $data): array { return $data; } }
No description
4 Replies
dissto
disstoβ€’2mo ago
I guess you could do something like that:
Select::make('company_registration_country')
->live()
->label(function (Get $get, Select $component, $state){
// if you simply want the country code
return 'Company Registration ' . $get('company_registration_country');

// or you probably want the actual name of the country as opposed to the country code
return 'Company Registration ' . ($component->getOptions()[$state] ?? null);
})
->options([
'FR' => 'France',
'CZ' => 'Czech Republic',
]),
Select::make('company_registration_country')
->live()
->label(function (Get $get, Select $component, $state){
// if you simply want the country code
return 'Company Registration ' . $get('company_registration_country');

// or you probably want the actual name of the country as opposed to the country code
return 'Company Registration ' . ($component->getOptions()[$state] ?? null);
})
->options([
'FR' => 'France',
'CZ' => 'Czech Republic',
]),
πŸ€”
Ed
Edβ€’2mo ago
Thanks @dissto !
LeandroFerreira
LeandroFerreiraβ€’2mo ago
Select::make('company_registration_country')
->live()
->label(fn (Select $component, ?string $state) => 'Company Registration '.(filled($state) ? $component->getOptionLabel() : ' Country'))
->options([
'FR' => 'France',
'CZ' => 'Czech Republic',
])
Select::make('company_registration_country')
->live()
->label(fn (Select $component, ?string $state) => 'Company Registration '.(filled($state) ? $component->getOptionLabel() : ' Country'))
->options([
'FR' => 'France',
'CZ' => 'Czech Republic',
])
Ed
Edβ€’2mo ago
Thank you 😊