F
Filament10mo ago
ericmp

How to access form component record in ->when fn?

with this code:
Forms\Components\DateTimePicker::make('paid_at')
->label(__('Paid at'))
->disabled()
->when(
value: function (Forms\Components\DateTimePicker $dateTimePicker, Payment $record): bool {
return $record->has_users;
},
callback: function (Forms\Components\DateTimePicker $dateTimePicker): Forms\Components\DateTimePicker {
return $dateTimePicker
->hint(function (Payment $record): ?string {
if (is_null($record->paid_by_id)) {
return null;
}

return "{$record->paid_at->diffForHumans()}";
})
->hintIcon('heroicon-s-question-mark-circle')
->hintIconTooltip(function (Payment $record): ?string {
if (is_null($record->paid_by_id)) {
return null;
}

return __('Paid by') . " {$record->paidBy->getFullname()} ({$record->paidBy->email}), {$record->paid_at->diffForHumans()}";
})
;
},
default: function (Forms\Components\DateTimePicker $dateTimePicker): Forms\Components\DateTimePicker {
return $dateTimePicker
->hint('false') // ⚠️ todo
;
}
)
,
Forms\Components\DateTimePicker::make('paid_at')
->label(__('Paid at'))
->disabled()
->when(
value: function (Forms\Components\DateTimePicker $dateTimePicker, Payment $record): bool {
return $record->has_users;
},
callback: function (Forms\Components\DateTimePicker $dateTimePicker): Forms\Components\DateTimePicker {
return $dateTimePicker
->hint(function (Payment $record): ?string {
if (is_null($record->paid_by_id)) {
return null;
}

return "{$record->paid_at->diffForHumans()}";
})
->hintIcon('heroicon-s-question-mark-circle')
->hintIconTooltip(function (Payment $record): ?string {
if (is_null($record->paid_by_id)) {
return null;
}

return __('Paid by') . " {$record->paidBy->getFullname()} ({$record->paidBy->email}), {$record->paid_at->diffForHumans()}";
})
;
},
default: function (Forms\Components\DateTimePicker $dateTimePicker): Forms\Components\DateTimePicker {
return $dateTimePicker
->hint('false') // ⚠️ todo
;
}
)
,
i get:
Too few arguments to function App\Filament\Resources\PaymentResource::App\Filament\Resources\{closure}(), 1 passed in /Users/eric/Documents/projects/suite_bg_wrapper/bgpay/vendor/laravel/framework/src/Illuminate/Conditionable/Traits/Conditionable.php on line 23 and exactly 2 expected
Too few arguments to function App\Filament\Resources\PaymentResource::App\Filament\Resources\{closure}(), 1 passed in /Users/eric/Documents/projects/suite_bg_wrapper/bgpay/vendor/laravel/framework/src/Illuminate/Conditionable/Traits/Conditionable.php on line 23 and exactly 2 expected
the error line is this one:
value: function (Forms\Components\DateTimePicker $dateTimePicker, Payment $record): bool {
value: function (Forms\Components\DateTimePicker $dateTimePicker, Payment $record): bool {
the objective is to show hints "A" or hints "B" depending if the has_users record field is true or not
22 Replies
ericmp
ericmpOP10mo ago
bump
sandofgods
sandofgods10mo ago
the second argument should be a callback or null, you provide a Model, try
funtion(Payment $record){
//your logic and return here
}
funtion(Payment $record){
//your logic and return here
}
in place for the second argument.
ericmp
ericmpOP10mo ago
okay i changed it:
ericmp
ericmpOP10mo ago
->when(
value: function (Payment $record): bool {
...
->when(
value: function (Payment $record): bool {
...
but i get:
App\Filament\Resources\PaymentResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\Payment, Filament\Forms\Components\DateTimePicker given, called in
App\Filament\Resources\PaymentResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\Payment, Filament\Forms\Components\DateTimePicker given, called in
No description
ericmp
ericmpOP10mo ago
u meant to do it this way? @sandofgods that is why i also passed it in the DateTimePicker component maybe, somehow, i can access the current record through the DateTimePicker - i ve already tried it but i couldnt do it so far but seems in the value's callback we cannot pass in the record :/ is there any way to achieve it?
sandofgods
sandofgods10mo ago
Ok if i get it, you want to access the Component instance, you can do it like you do (check https://filamentphp.com/docs/3.x/forms/advanced#injecting-the-current-form-component-instance). The error you get is relative to the callback, not the value, so you should have with this order:
use Filament\Forms\Components\Component;

->when(
value: function(Payment $record):bool{
//your logic
},
callback: function(Payment $record, Component $component){
//your logic
}
)
use Filament\Forms\Components\Component;

->when(
value: function(Payment $record):bool{
//your logic
},
callback: function(Payment $record, Component $component){
//your logic
}
)
Off course you can be more specific when calling the Component object like you have do, its should be work, you just have to keep the right order for your variables
ericmp
ericmpOP9mo ago
i want to access the record and show some hints, or if false, show other hints if the record has_users is true so i try to:
value: function (Payment $record): bool {
value: function (Payment $record): bool {
but that gives me:
App\Filament\Resources\PaymentResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\Payment, Filament\Forms\Components\DateTimePicker given, called in
App\Filament\Resources\PaymentResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\Payment, Filament\Forms\Components\DateTimePicker given, called in
thats the line that doesnt work the :callback param just works, i dont have to change it, i have to change the :value param bump bump ^^
awcodes
awcodes9mo ago
$component->getLivewire()->getRecord() Something like that. Record probably isn’t injected into when.
ericmp
ericmpOP9mo ago
with this code:
value: function (Forms\Components\DateTimePicker $dateTimePicker): bool {
dd(
$dateTimePicker->getLivewire()->getRecord()
);
},
value: function (Forms\Components\DateTimePicker $dateTimePicker): bool {
dd(
$dateTimePicker->getLivewire()->getRecord()
);
},
i get:
Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization
Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization
if instead of the dd i return a boolean, it doesnt crash, so yeah is the dd line the one that crashes hmmm
awcodes
awcodes9mo ago
Should probably be $component instead of $dateTimePicker. The variable names matter in the callbacks. $livewire might be available directly to if you inject it. Not sure. That way you don’t to call it off the component.
ericmp
ericmpOP9mo ago
also tried:
value: function ($component): bool {
value: function ($component): bool {
i get the same hmm okay ill try:
value: function ($livewire): bool {
value: function ($livewire): bool {
awcodes
awcodes9mo ago
Maybe too early to get it out. Probably why $record isn’t available.
ericmp
ericmpOP9mo ago
same error oh hmmm
awcodes
awcodes9mo ago
I’m thinking it would be better to do all this inside ->hint() instead of ->when()
ericmp
ericmpOP9mo ago
also thought bout it, but yeah i'll add a lot of more chained functions i each of them ill have to do this check that is why i wanted to do it using ->when
awcodes
awcodes9mo ago
If you have repeated functionality just do the callback as a separate method that can be used in any field. You could also macro the field so it’s just already part of it. That would keep you from having to repeat yourself.
ericmp
ericmpOP9mo ago
okay hmm i guess ill have to do that, so u sure is not possible to get the record there? 🤔
awcodes
awcodes9mo ago
I’m not sure at all, but the reason to not use ->hint() is duplication then using ->when doesn’t actually solve your problem.
ericmp
ericmpOP9mo ago
okay yeah i wont do it using ->when then ill try to get the record one more time, if i cant, ill do it the other way
awcodes
awcodes9mo ago
To be 100% honest though. I’m not totally sure what when() does on a form field. Do you have a doc link that made you choose it?
ericmp
ericmpOP9mo ago
uff dont remember, let me checkout docs real quick maybe i just did -> with my ide and found the method
awcodes
awcodes9mo ago
Yea. I’m not seeing it, which makes me think that although it’s showing in the ide, it’s probably an internal method and maybe not safe to use in the way you’re trying to use it. Which also explains why $record probably isn’t injected.
Want results from more Discord servers?
Add your server