Dynamic Form Publish Resource Action

I have a Page Resource with a publish_at Date-Field. For CreatePage I want to display different Save-Actions. For Example: 1. If publish_at is unset there should be a Save as Draft-Action and a Save and Publish Now-Action 2. If publish_at is set in the future, there should be a Save and Publish Later-Action Is there a way to dynamically change the Form's Actions based on the Form's data live? Can someone guide me to where to start?
10 Replies
toeknee
toeknee4w ago
Not changing the actions, you can handle the same action and change the result. So if publish_at then set publish later = true tc.
mmoollllee
mmoolllleeOP4w ago
True! But it would be nice if the Action-Button's label could change to indicate the resulting action.
toeknee
toeknee4w ago
Why not just add two buttons?
mmoollllee
mmoolllleeOP4w ago
Sure! But can I hide/disable a ActionButton based on field's data?
toeknee
toeknee4w ago
I think so, you can use ->visible
mmoollllee
mmoolllleeOP4w ago
This does not work as $record is null as long as it is not created yet and does not listen to unsaved changes. Is there a way to make this work "live"?
Actions\Action::make('saveAsDraft')
->label('Save as Draft')
->action('saveAsDraft')
->visible(fn (Model $record) => $record->publish_at === null),
Actions\Action::make('saveAndPublish')
->label('Save and Publish')
->action('saveAndPublish')
->visible(fn (Model $record) => $record->publish_at === null || $record->publish_at->isPast()),
Actions\Action::make('saveAndPublishLater')
->label(fn () => 'Save and publish on ' . $this->record->publish_at->format('d-m-Y'))
->action('saveAndPublishLater')
->visible(fn (Model $record) => $record->publish_at->isFuture()),
Actions\Action::make('saveAsDraft')
->label('Save as Draft')
->action('saveAsDraft')
->visible(fn (Model $record) => $record->publish_at === null),
Actions\Action::make('saveAndPublish')
->label('Save and Publish')
->action('saveAndPublish')
->visible(fn (Model $record) => $record->publish_at === null || $record->publish_at->isPast()),
Actions\Action::make('saveAndPublishLater')
->label(fn () => 'Save and publish on ' . $this->record->publish_at->format('d-m-Y'))
->action('saveAndPublishLater')
->visible(fn (Model $record) => $record->publish_at->isFuture()),
toeknee
toeknee4w ago
Actions\Action::make('saveAsDraft')
->label('Save as Draft')
->action('saveAsDraft')
->visible(fn (?Model $record) => $record?->publish_at === null),
Actions\Action::make('saveAndPublish')
->label('Save and Publish')
->action('saveAndPublish')
->visible(fn (?Model $record) => $record?->publish_at === null || $record?->publish_at->isPast()),
Actions\Action::make('saveAndPublishLater')
->label(fn () => 'Save and publish on ' . $this->record?->publish_at->format('d-m-Y'))
->action('saveAndPublishLater')
->visible(fn (?Model $record) => $record?->publish_at->isFuture()),
Actions\Action::make('saveAsDraft')
->label('Save as Draft')
->action('saveAsDraft')
->visible(fn (?Model $record) => $record?->publish_at === null),
Actions\Action::make('saveAndPublish')
->label('Save and Publish')
->action('saveAndPublish')
->visible(fn (?Model $record) => $record?->publish_at === null || $record?->publish_at->isPast()),
Actions\Action::make('saveAndPublishLater')
->label(fn () => 'Save and publish on ' . $this->record?->publish_at->format('d-m-Y'))
->action('saveAndPublishLater')
->visible(fn (?Model $record) => $record?->publish_at->isFuture()),
But you'll need to condition the actions, and ignore the visible I'd say. Validate failt o publish if X
mmoollllee
mmoolllleeOP4w ago
Thank you for the hints! Now I see the buttons! But so far I'd have to inform the user after click that what he's trying to do does not make sense. I'd just like to build the Actions in a way, that the user can't even go wrong 😉 So I tryed another approach to make the Date-Field ->live() and use ->visible(fn (?Get $get): bool => $get('publishing_begins_at') === null), But that results in Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization So another idea could be to change the Actions from the Field via ->afterStateUpdated()? Is there a chance to access the ActionButtons via Javascript?
toeknee
toeknee4w ago
The problem is the buttons are outside the form so they are not reactive because theres not really need too.
mmoollllee
mmoolllleeOP4w ago
Thank you for your help! I've posted a feature request on Github to see if this might be changed in the future 🙂 https://github.com/filamentphp/filament/discussions/15548
GitHub
Reactive Form Actions · filamentphp filament · Discussion #15548
I have a Page Resource with a publish_at Date-Field. For CreatePage I want to display different Save-Actions. For Example: If publish_at is unset there should be a Save as Draft-Action and a Save a...

Did you find this page helpful?