How to access form component record in ->when fn?
with this code:
i get:
the error line is this one:
the objective is to show hints "A" or hints "B" depending if the
has_users
record field is true or not22 Replies
bump
the second argument should be a callback or null, you provide a Model, try
in place for the second argument.
okay i changed it:
but i get:
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?
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:
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
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:
but that gives me:
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 ^^$component->getLivewire()->getRecord()
Something like that. Record probably isn’t injected into when.
with this code:
i get:
if instead of the dd i return a boolean, it doesnt crash, so yeah is the dd line the one that crashes hmmm
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.
also tried:
i get the same
hmm okay ill try:
Maybe too early to get it out. Probably why $record isn’t available.
same error
oh hmmm
I’m thinking it would be better to do all this inside ->hint() instead of ->when()
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
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.
okay hmm i guess ill have to do that, so u sure is not possible to get the record there? 🤔
I’m not sure at all, but the reason to not use ->hint() is duplication then using ->when doesn’t actually solve your problem.
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
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?
uff dont remember, let me checkout docs real quick
maybe i just did -> with my ide and found the method
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.