Hide a Toggle field depending on Select option
For some reason, I can't make it work. Here's what I did:
Select::make('roles')
->relationship('roles', 'name')
->live(),
Toggle::make('is_admin')
->hidden(fn ($record) => !$record->roles->pluck('name')->contains('Vendor'))
30 Replies
Im not sure what
$get('roles')
returns, can you try this and show what it returns 🙂 ?
@RenzoHi @CodeWithDennis, thanks for the reply! So I have 3 roles, Admin, Client, and Vendor. I was hoping to hide some fields if the role selected was Vendor. I'm not sure if it's possible to do if you're using a relationship Select field.
Try the code i sent and send me the response of the dump
It depents on the Select::make('roles') right? You should hide it based on the current state of the form using $get and not the $record
Yep, thats what i sent. 🙂
sorry for echoing your answer. I just read the question and haven't read yours
Np, all he has to do now is check the value of the get and compare it to hide it.
Assuming it returns a string
Sorry, what do you mean by response of the dump?
Sorry, been coding for 5 months only 😄
Did you copy my code and try it?
Yes, but it changes the UI, hold on let me send it here.
Yes, it will dump the value (bottom of the screen probably)
You should also read this section to undestand:
https://filamentphp.com/docs/2.x/forms/advanced#dependant-fields--components
Here's what I got
Use ``` code here \ ```
I'll try this one out, does this work with v3 as well?
select some role
yea
By the way, tried this one too but the Toggle field still shows up.
Please listen 😛
Select a role, and tell me what does the dump return
Oh still the same, doesn't return anything when I select a role.
did you use my code?
Yes, this one
Select::make('roles')
->relationship('roles', 'name')
->reactive(),
Toggle::make('user_preferred')
->hidden(fn ($get) => dd($get('roles')))
Oh it worked! Tho when I try to select another role again it still shows up. I'll try to do the adjustments 🙂
Just try to mess around with the conditions 🙂
Oh wait, it shows up when I try to pick any role which is weird
its probably the OR condition
Replace
||
or remove it to test
You know what, just change it to:
Yeah, it still does the same behavior :/ weird, anywho, I'll just stick to boolean like on the documentation
👍
Thanks 🙂