Error when prefilling from query string in resource with persistTabInQueryString enabled

Hi, I get the following error when persisting tabs in query string enabled when creating a form or uploading files inside the form: https://flareapp.io/share/xPQ3Xol5#F113 The query strings looks like this: ?event_start=2023-03-04&event_end=2023-03-04&tab=tabs-evenement-tab, and the code for filling like this (not sure if it is the correct way to handle it):
class CreatePost extends CreateRecord
{
protected static string $resource = PostResource::class;

protected function afterFill()
{
$validator = Validator::make(
Request::query(),
[
'event_start' => 'required|date:Y-m-d',
'event_end' => 'date:Y-m-d',
]
);
if ($validator->fails()) {
return;
}

$data = $validator->validated();
$this->form->fill([
'has_event' => true,
'event_start' => $data['event_start'] . ' 12:00',
'event_end' => isset($data['event_end']) ? $data['event_end'] . ' 12:00' : null,
]);
}
}
class CreatePost extends CreateRecord
{
protected static string $resource = PostResource::class;

protected function afterFill()
{
$validator = Validator::make(
Request::query(),
[
'event_start' => 'required|date:Y-m-d',
'event_end' => 'date:Y-m-d',
]
);
if ($validator->fails()) {
return;
}

$data = $validator->validated();
$this->form->fill([
'has_event' => true,
'event_start' => $data['event_start'] . ' 12:00',
'event_end' => isset($data['event_end']) ? $data['event_end'] . ' 12:00' : null,
]);
}
}
Flare
Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization - The error occurred at https://radiotherapie.test/berichten/aanmaken
10 Replies
Arjen
ArjenOP2y ago
And the code for the tabs:
Forms\Components\Tabs::make('Tabs')
->id('tabs')
->persistTabInQueryString('tab') // <-- culprit
->tabs([
Forms\Components\Tabs\Tab::make('Documenten')
->schema([
Forms\Components\Toggle::make('has_documents')
->label('Documenten?')
->inline(false)
->default(false)
->reactive(),
Forms\Components\Repeater::make('documents')
->label('Documenten')
->relationship()
->schema([
Forms\Components\Radio::make('type')
Forms\Components\FileUpload::make('file')
->label('Bestand')
->directory('documents')
->preserveFilenames()
->removeUploadedFileButtonPosition('right')
->required(),
])
->visible(fn (\Closure $get) => $get('has_documents'),
]),
Forms\Components\Tabs\Tab::make('Evenement')
->schema([
Forms\Components\Toggle::make('has_event')
->label('Evenement?')
->inline(false)
->default(false)
->reactive(),
Forms\Components\TextInput::make('event_start')
->label('Start')
->type('datetime-local')
->required()
->columns(2)
->visible(fn (\Closure $get) => $get('has_event')),
Forms\Components\TextInput::make('event_end')
->label('Eind')
->type('datetime-local')
->columns(2)
->visible(fn (\Closure $get) => $get('has_event')),
]),
]),
Forms\Components\Tabs::make('Tabs')
->id('tabs')
->persistTabInQueryString('tab') // <-- culprit
->tabs([
Forms\Components\Tabs\Tab::make('Documenten')
->schema([
Forms\Components\Toggle::make('has_documents')
->label('Documenten?')
->inline(false)
->default(false)
->reactive(),
Forms\Components\Repeater::make('documents')
->label('Documenten')
->relationship()
->schema([
Forms\Components\Radio::make('type')
Forms\Components\FileUpload::make('file')
->label('Bestand')
->directory('documents')
->preserveFilenames()
->removeUploadedFileButtonPosition('right')
->required(),
])
->visible(fn (\Closure $get) => $get('has_documents'),
]),
Forms\Components\Tabs\Tab::make('Evenement')
->schema([
Forms\Components\Toggle::make('has_event')
->label('Evenement?')
->inline(false)
->default(false)
->reactive(),
Forms\Components\TextInput::make('event_start')
->label('Start')
->type('datetime-local')
->required()
->columns(2)
->visible(fn (\Closure $get) => $get('has_event')),
Forms\Components\TextInput::make('event_end')
->label('Eind')
->type('datetime-local')
->columns(2)
->visible(fn (\Closure $get) => $get('has_event')),
]),
]),
LeandroFerreira
Why not TextInput::make('event_start')->rules() ?
Arjen
ArjenOP2y ago
Not sure what you mean by rules()
LeandroFerreira
you can also use any other Laravel validation rules, including custom validation rules. https://filamentphp.com/docs/2.x/forms/validation#getting-started
Filament
Validation - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
Arjen
ArjenOP2y ago
I don't understand what validation rules have to with my problem. My query string gets validated fine, but I get a Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization when have the following enabled: ->persistTabInQueryString('tab')
LeandroFerreira
afterStateHydrated in an option to to customize what happens after a field is hydrated: https://filamentphp.com/docs/2.x/forms/advanced#hydration
Filament
Advanced - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
LeandroFerreira
May be you can try another way
Arjen
ArjenOP2y ago
Tried it that way but it's still not working. It seems like a bug in Filament. @Dan Harrin Any thoughts about it? Should I make an issue in the github repo?
Dan Harrin
Dan Harrin2y ago
sure, but please make a simple reproduction repository instead of the code you posted above
Arjen
ArjenOP2y ago
Sure!
Want results from more Discord servers?
Add your server