Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set hidden state on Action within form #15287

Closed
danjohnson95 opened this issue Jan 8, 2025 · 2 comments
Closed

Can't set hidden state on Action within form #15287

danjohnson95 opened this issue Jan 8, 2025 · 2 comments
Labels

Comments

@danjohnson95
Copy link

danjohnson95 commented Jan 8, 2025

Package

filament/forms

Package Version

v3.2.132

Laravel Version

v11.37.0

Livewire Version

v3.5.12

PHP Version

PHP 8.2.25

Problem description

When building a form that contains an action, I should be able to use the visibleOn() method on the action to only show the action on specified operations.

For example with the following code, I would expect the "Generate password" action to only be visible on the 'create' action.

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('password')
                    ->label('Password')
                    ->password()
                    ->required()
                    ->revealable()
                    ->columnSpanFull(),
                
                Action::make('generate_password')
                    ->label('Generate password')
                    ->action(fn (Set $set) => $set('password', Str::random(16)))
                    ->toFormComponent()
                    ->visibleOn(['create']),
            ]);
    }

Currently, the "Generate password" action is incorrectly shown on both the "Create" and "Edit" operation.

Calling visibleOn(), visible() or hidden() has no effect on the Action.

Expected behavior

I would expect the above example to only show the "Generate password" action on the "create" operation, because of the call to ->visibleOn(['create']).

Steps to reproduce

  • Create a Form
  • Add an Action to the form, and call ->visibleOn(['create']) on it
  • See that the Action is still visible on the Edit page.

Reproduction repository (issue will be closed if this is not valid)

https://github.com/danjohnson95/filament-issue

Relevant log output

No response

@danharrin
Copy link
Member

toFormComponent() is an internal method for wrapping an action in a form component, and it is not documented. It is used by the Actions::make() form component as part of the rendering process.

As per the documentation, I suggest that you use Actions::make() to render actions in a form, otherwise I can't guarantee other things will work as you want them to either. Using this component will also unlock other features such as controlling action alignment.

public static function form(Form $form): Form
{
    return $form
        ->schema([
            TextInput::make('password')
                ->label('Password')
                ->password()
                ->required()
                ->revealable()
                ->columnSpanFull(),
            
            Actions::make([
                Action::make('generate_password')
                    ->label('Generate password')
                    ->action(fn (Set $set) => $set('password', Str::random(16))),
            ])->visibleOn(['create']),
        ]);
}

@danharrin danharrin closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2025
@github-project-automation github-project-automation bot moved this from Todo to Done in Roadmap Jan 9, 2025
@danjohnson95
Copy link
Author

Thanks Dan, Actions::make() was exactly what I wanted and I missed that in the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants