Dynamic form creation for custom action

Hello - is it possible to create a form with a dynamic number of fields if I am using a custom action on an edit page? For example:
//
Actions\Action::make('customDynamicForm)
->form([
foreach($data as $section) {
// ... generate form
}
])
//
Actions\Action::make('customDynamicForm)
->form([
foreach($data as $section) {
// ... generate form
}
])
I've tried using a repeater but I don't want the user selecting the number of sections. I want to control the number sections and pre-populate them with information
27 Replies
Dennis Koch
Dennis Koch2y ago
You should be able to do something like this:
->action(function ($livewire) {
$record = $livewire->getRecord();

return [...fields...];
})
->action(function ($livewire) {
$record = $livewire->getRecord();

return [...fields...];
})
bwurtz999
bwurtz999OP2y ago
Then how would I generate a form using those fields? Does the form() call have access to what the action() returns?
Dennis Koch
Dennis Koch2y ago
What do you mean? You return an array with your fields. How can it? The actions runs after the form. Do you want to set the form on the EditPage from within the Action? 🤔
bwurtz999
bwurtz999OP2y ago
Maybe not, but I can't think of a better way I need something a little more complex than a relationship manager Is there a way to use a custom footer on the bottom of an edit page? Everything I'm trying to modify is related to the item, so I believe using the edit page makes sense Or maybe a custom view for a relationship manager and then write my own logic?
Dennis Koch
Dennis Koch2y ago
Maybe explain what you are trying to achieve
bwurtz999
bwurtz999OP2y ago
Hey sorry I missed your reply. What I have is an app that has a menu of food items. Each item can have options added to it (i.e.: toppings on a sandwich). The manager needs to be able to select which options (and by extension, option categories) are available for the customer to see when they click on at item. I don't want all the options showing up. Only the ones the manager designates for that specific item So what I need is a form that shows all the option categories (and options) to the manager So they can select whether or not that category of options is visible to the customer And then select which options within that category are visible Meaning, maybe not all toppings are available for a specific type of sandwich
Dennis Koch
Dennis Koch2y ago
Can't that be solved with just a CheckboxList?
bwurtz999
bwurtz999OP2y ago
Yes it could! But how would I set up a dynamic number of lists? There could be one category or 20
Dennis Koch
Dennis Koch2y ago
Those categories need to be stored somewhere right? Then just give them as an input to CheckboxList
bwurtz999
bwurtz999OP2y ago
Yes they are stored I'm confused how to use the form([]) call dynamically. Does it accept a callback?
Dennis Koch
Dennis Koch2y ago
Yes. That's what I explained in my first answer.
bwurtz999
bwurtz999OP2y ago
Because then I could use a loop to generate the form that I need and then return that, right?
Dennis Koch
Dennis Koch2y ago
But what do you need the action for? I though this is just set on the EditPage You don't even need a Loop with a Checkbox List Oh I think I get it now. You want Categories and each have options underneath that.
bwurtz999
bwurtz999OP2y ago
yes And I'm trying to use a modal on the page to achieve this
Dennis Koch
Dennis Koch2y ago
Can't you just work with dependant fields? Make a group for every categories options and toggle visibility if the category is checked No need for a modal then
bwurtz999
bwurtz999OP2y ago
There are other fields that need to be editable as well. Two could be a toggle but another is a numeric value which I don't think can be modified from a table like the toggle can do can it?
Dennis Koch
Dennis Koch2y ago
Wait. What does a table have to do with this now? And what's the issue with other fields? You can put as many as you want on an edit page?
bwurtz999
bwurtz999OP2y ago
Oh you're saying to just make it a part of the actual EditItem form
Dennis Koch
Dennis Koch2y ago
Yes
bwurtz999
bwurtz999OP2y ago
Ahhh
Dennis Koch
Dennis Koch2y ago
Group the category options and then do something like:
$fields = [
CheckboxList::make('categories')->options($categories),
];

foreach ($categories as $category) {
$fields[] = Group::make()
->visible(fn ($get) => in_array($category, $get('categories'))
->schema()
$fields = [
CheckboxList::make('categories')->options($categories),
];

foreach ($categories as $category) {
$fields[] = Group::make()
->visible(fn ($get) => in_array($category, $get('categories'))
->schema()
bwurtz999
bwurtz999OP2y ago
Oh thank you! Where can I find Group in the docs? I searched for it but all that comes up is Navigation and Actions
Dennis Koch
Dennis Koch2y ago
Sorry, there is no Group. Use Card, Section, Grid
bwurtz999
bwurtz999OP2y ago
Oh ok And I would use this in public static function form? On the ItemResource?
Dennis Koch
Dennis Koch2y ago
Yes
bwurtz999
bwurtz999OP2y ago
Got it Thank you! This has been so helpful
Dennis Koch
Dennis Koch2y ago
you’re welcome
Want results from more Discord servers?
Add your server