F
Filament3mo ago
nowak

Preserve data across form wizard steps

I have a 2 step form wizard, with a meal_type_id field in step 1, which I want to preserve for step 2. This is what I have tried so far. In the meal_type_id field, I have tried to set the $state to a selected_meal_type_id variable like so:
Select::make('meal_type_id')
->options(function (Get $get) {
$group = Group::find($get('group_id'));
return $group->mealTypes->pluck('name', 'id');
})
->live()
->afterStateUpdated(function (Set $set, $state) {
$set('selected_meal_type_id', $state);
})
->required(),
Select::make('meal_type_id')
->options(function (Get $get) {
$group = Group::find($get('group_id'));
return $group->mealTypes->pluck('name', 'id');
})
->live()
->afterStateUpdated(function (Set $set, $state) {
$set('selected_meal_type_id', $state);
})
->required(),
Then in my repeater function in step 2, I want to Get this data in a Select field:
public static function getItemsRepeater(): Repeater
{
return Repeater::make('productUserOrders')
->relationship()
->schema([
Select::make('product_id')
->label('Product')
->options(function (Get $get, $state) {
$mealTypeId = $get('selected_meal_type_id');
if(! $mealTypeId) {
return [];
}
$mealType = MealType::find($mealTypeId);
return $mealType->products->pluck('name', 'id');
})
->required()
->live()
->afterStateUpdated(fn ($state, Set $set) => $set('price', Product::find($state)?->price ?? 0))
->distinct()
->columnSpan([
'md' => 5,
]),
])
->hiddenLabel()
->columns([
'md' => 10,
])
->required();
}
public static function getItemsRepeater(): Repeater
{
return Repeater::make('productUserOrders')
->relationship()
->schema([
Select::make('product_id')
->label('Product')
->options(function (Get $get, $state) {
$mealTypeId = $get('selected_meal_type_id');
if(! $mealTypeId) {
return [];
}
$mealType = MealType::find($mealTypeId);
return $mealType->products->pluck('name', 'id');
})
->required()
->live()
->afterStateUpdated(fn ($state, Set $set) => $set('price', Product::find($state)?->price ?? 0))
->distinct()
->columnSpan([
'md' => 5,
]),
])
->hiddenLabel()
->columns([
'md' => 10,
])
->required();
}
But it seems like the data is not preserved from step 1 to step 2. What am I doing wrong?
3 Replies
nowak
nowak3mo ago
I have also tried to Get the meal_type_id directly, but I only get null on step 2.
nowak
nowak3mo ago
Wow, I read this documentation, but when I read it I thought parent meant fields within the repeater, so I didn't try this. Thank you so much!!
Want results from more Discord servers?
Add your server
More Posts
Login not working for role that only has access to single panelI have 3 panels, admin, app, and client. When I try to log in to the app using an account that has aWhat is the difference between these two options on a select filter?I've seen this done both ways but unsure which to use? ```php SelectFilter::make('tags') ->optimake tenant switching menu not clickable if someone only has one tenant?How can I make the tenant switching menu not be clickable or appear as a drop down when someone onlyReordoring a table makes the edit buttons disappearHI, I was happy to find that we can reorder the elements of a table in a Resource. However, when I shook into notification received eventis there anyway to hook into notification received event? or that event even exist ? i just want to Pagination inside modalAny idea how to implement pagination inside modal? It would be ideal to implement infinite scrollingField state is lost on save to JSON columns when invisibleHi all. Not sure if I'm doing something wrong, but fields that are conditionally hidden using `->visSet dynamic table columnI want to print a specific table column on a row. If row's type is 1, set a text input column. OtherHow can I test a form from a modal? (simple resource)I have some resources made with --simple flag, so they don't have `Create`, `Edit` or `View` pages, what is the best practice for adding a line break in the table?i didnt know how, but i have created a file with only <br> inside and called it in my filament table