Form with Wizard not saving relations with non-default statePaths
I've got the following code:
class PlaceServiceForm extends Component implements HasForms
{
use InteractsWithForms;
public ?array $data = [];
public function mount(): void
{
$this->form->fill();
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Wizard::make([
Forms\Components\Wizard\Step::make('Aanbod plaatsen')
->schema([
// ...
Forms\Components\CheckboxList::make('categories')
->label('Categorieën (max. 3)')
->relationship(
titleAttribute: 'name',
),
// ...
])
->statePath('data.service')
->model(Models\Service::class),
Forms\Components\Wizard\Step::make('Organisatiegegevens')
->schema(OrganizationRegistration::getFormSchema())
->statePath('data.organization')
->model(Models\Organization::class),
Forms\Components\Wizard\Step::make('Logingegevens')
->schema(UserRegistration::getFormSchema())
->statePath('data.user')
->model(Models\User::class),
])
->submitAction(new HtmlString(Blade::render(<<<'BLADE'
<x-filament::button type="submit">
Plaatsen
</x-filament::button>
BLADE))),
])
->statePath('data');
}
class PlaceServiceForm extends Component implements HasForms
{
use InteractsWithForms;
public ?array $data = [];
public function mount(): void
{
$this->form->fill();
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Wizard::make([
Forms\Components\Wizard\Step::make('Aanbod plaatsen')
->schema([
// ...
Forms\Components\CheckboxList::make('categories')
->label('Categorieën (max. 3)')
->relationship(
titleAttribute: 'name',
),
// ...
])
->statePath('data.service')
->model(Models\Service::class),
Forms\Components\Wizard\Step::make('Organisatiegegevens')
->schema(OrganizationRegistration::getFormSchema())
->statePath('data.organization')
->model(Models\Organization::class),
Forms\Components\Wizard\Step::make('Logingegevens')
->schema(UserRegistration::getFormSchema())
->statePath('data.user')
->model(Models\User::class),
])
->submitAction(new HtmlString(Blade::render(<<<'BLADE'
<x-filament::button type="submit">
Plaatsen
</x-filament::button>
BLADE))),
])
->statePath('data');
}
1 Reply
Previous file continued:
The data retrieved in the submit method looks like this:
I can't get the selected categories saved in the $service record I created. Any idea what I'm doing wrong? The categories relationship is many to many. I also have a normal ServiceResource in my panel where the categories checkboxlist works as it should, however without any changes to the statePath.
public function submit(): void
{
DB::transaction(function () {
$data = $this->form->getState();
// Creating user and organization with data.organization and data.user first...
$service = Models\Service::create($data['data']['service']);
$this->form->model($service)->statePath('data.service')->saveRelationships(); // This does not work, even without ->statePath()...
});
}
}
public function submit(): void
{
DB::transaction(function () {
$data = $this->form->getState();
// Creating user and organization with data.organization and data.user first...
$service = Models\Service::create($data['data']['service']);
$this->form->model($service)->statePath('data.service')->saveRelationships(); // This does not work, even without ->statePath()...
});
}
}
$data = [
'data' => [
'user' => [...],
'organization' => [...],
'service' => [...],
],
];
$data = [
'data' => [
'user' => [...],
'organization' => [...],
'service' => [...],
],
];