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
Not changing the actions, you can handle the same action and change the result. So if publish_at then set publish later = true tc.
True! But it would be nice if the Action-Button's label could change to indicate the resulting action.
Why not just add two buttons?
Sure! But can I hide/disable a ActionButton based on field's data?
I think so, you can use ->visible
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"?
But you'll need to condition the actions, and ignore the visible I'd say. Validate failt o publish if X
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?The problem is the buttons are outside the form so they are not reactive because theres not really need too.
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...