F
Filament16mo ago
Renzo

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
BlackShadow
BlackShadow16mo ago
Select::make('roles')
->native(false) // Make it look pretty
->relationship('roles', 'name')
->reactive(),

Toggle::make('is_admin')
->hidden(fn ($get) => dd($get('roles')),
Select::make('roles')
->native(false) // Make it look pretty
->relationship('roles', 'name')
->reactive(),

Toggle::make('is_admin')
->hidden(fn ($get) => dd($get('roles')),
Im not sure what $get('roles') returns, can you try this and show what it returns 🙂 ? @Renzo
Renzo
RenzoOP16mo ago
Hi @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.
BlackShadow
BlackShadow16mo ago
Try the code i sent and send me the response of the dump
Homd
Homd16mo ago
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
BlackShadow
BlackShadow16mo ago
Yep, thats what i sent. 🙂
Homd
Homd16mo ago
sorry for echoing your answer. I just read the question and haven't read yours
BlackShadow
BlackShadow16mo ago
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
Renzo
RenzoOP16mo ago
Sorry, what do you mean by response of the dump? Sorry, been coding for 5 months only 😄
BlackShadow
BlackShadow16mo ago
Did you copy my code and try it?
Select::make('roles')
->native(false) // Make it look pretty
->relationship('roles', 'name')
->reactive(),

Toggle::make('is_admin')
->hidden(fn ($get) => $get('roles') == 'Vendor'), // Vendor or vendor (what ever the value is)
Select::make('roles')
->native(false) // Make it look pretty
->relationship('roles', 'name')
->reactive(),

Toggle::make('is_admin')
->hidden(fn ($get) => $get('roles') == 'Vendor'), // Vendor or vendor (what ever the value is)
Renzo
RenzoOP16mo ago
Yes, but it changes the UI, hold on let me send it here.
BlackShadow
BlackShadow16mo ago
Yes, it will dump the value (bottom of the screen probably)
Renzo
RenzoOP16mo ago
Here's what I got
BlackShadow
BlackShadow16mo ago
Use ``` code here \ ```
Renzo
RenzoOP16mo ago
I'll try this one out, does this work with v3 as well?
No description
BlackShadow
BlackShadow16mo ago
select some role yea
Renzo
RenzoOP16mo ago
By the way, tried this one too but the Toggle field still shows up.
BlackShadow
BlackShadow16mo ago
Please listen 😛 Select a role, and tell me what does the dump return
Renzo
RenzoOP16mo ago
Oh still the same, doesn't return anything when I select a role.
BlackShadow
BlackShadow16mo ago
did you use my code?
Renzo
RenzoOP16mo ago
Yes, this one Select::make('roles') ->relationship('roles', 'name') ->reactive(), Toggle::make('user_preferred') ->hidden(fn ($get) => dd($get('roles')))
BlackShadow
BlackShadow16mo ago
Select::make('roles')
->relationship('roles', 'name')
->live(),

Toggle::make('is_admin')
->hidden(fn ($get) => $get('roles') == 'Vendor' || !filled($get('roles')),
Select::make('roles')
->relationship('roles', 'name')
->live(),

Toggle::make('is_admin')
->hidden(fn ($get) => $get('roles') == 'Vendor' || !filled($get('roles')),
Renzo
RenzoOP16mo ago
Oh it worked! Tho when I try to select another role again it still shows up. I'll try to do the adjustments 🙂
BlackShadow
BlackShadow16mo ago
Just try to mess around with the conditions 🙂
Renzo
RenzoOP16mo ago
Oh wait, it shows up when I try to pick any role which is weird
BlackShadow
BlackShadow16mo ago
its probably the OR condition Replace || or remove it to test You know what, just change it to:
Toggle::make('is_admin')
->visible(fn ($get) => $get('roles') !== 'Vendor' && filled($get('roles'))),
Toggle::make('is_admin')
->visible(fn ($get) => $get('roles') !== 'Vendor' && filled($get('roles'))),
Renzo
RenzoOP16mo ago
Yeah, it still does the same behavior :/ weird, anywho, I'll just stick to boolean like on the documentation
BlackShadow
BlackShadow16mo ago
👍
Renzo
RenzoOP16mo ago
Thanks 🙂
Want results from more Discord servers?
Add your server