Issue with Default Items in Edit Mode for Questions Step

Hi everyone, I'm encountering an issue with a form in FilamentPHP, specifically in the questions step. The form works perfectly in create mode but not in edit mode. Here’s a summary of the code and the problem I’m facing. Code Overview: 1. getDefaultItems Method: - Retrieves parent and child questions from the database. - Creates an array of default items. 2. Form Configuration for Questions Step: - A Wizard Step with a Repeater for projectQuestions. - Contains parent_id, answer, conditional image, and description fields. - Nested Repeater for childQuestions with conditional fields. Code link: GitHub Gist Problem Description: - In create mode, the form initializes correctly. - In edit mode, default items are not loaded properly. I tried adding the afterStateHydrated method to call getDefaultItems in the main repeater. This works, but if there's already saved data, it doesn't load correctly because getDefaultItems returns the default data. Here’s an example of my attempt:
Forms\Components\Repeater::make('projectQuestions')
->label(__('lang.project_questions'))->hiddenLabel()
->relationship('projectQuestions')
->schema([...])
->afterStateHydrated(function ($state, callable $set) {
if (empty($state)) {
$set('projectQuestions', getDefaultItems());
}
})
Forms\Components\Repeater::make('projectQuestions')
->label(__('lang.project_questions'))->hiddenLabel()
->relationship('projectQuestions')
->schema([...])
->afterStateHydrated(function ($state, callable $set) {
if (empty($state)) {
$set('projectQuestions', getDefaultItems());
}
})
Issue: In edit mode, default items are set instead of loading saved data from the database. Question: How can I ensure that in edit mode, the form loads existing saved data correctly and only sets default items if the form is empty? Any help or pointers would be greatly appreciated. Thank you!
Gist
MyProjectsResource.php
GitHub Gist: instantly share code, notes, and snippets.
3 Replies
Dennis Koch
Dennis Koch2mo ago
I think you should use ->mutateRelationshipDataBeforeFillUsing() to fill default data.
Mstrihi
Mstrihi2mo ago
Hi, thank you but what I should add inside this function based to my code? the parent question back correct without use any method. the problem when I change in the answer of the parent should show the child questions.
Dennis Koch
Dennis Koch2mo ago
I'v never had the need for this, but I think you return $data which is the state for the Repeater
Want results from more Discord servers?
Add your server