F
Filament10mo ago
Mina

hiddenOn returns an error when creating a resource

So, on my app I had to add creation of whitelists. Before that you could only view the resource, you couldn't create it or edit it. Now, we had to add the creation of record. As you know, the view, edit and create are all using the same form. When creating a record now, we get an error: App\Filament\Resources\WhitelistResource::App\Filament\Resources\{closure}(): Argument #1 ($record) must be of type App\Models\v1\Whitelist, null given, called in /var/task/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35 I've looked through the code and realized it's because of this part: Forms\Components\TextInput::make('planRelation.name') ->label('Plan') ->formatStateUsing((function (Whitelist $record): string { return $record->planRelation->name ?? ''; })) ->hiddenOn(['edit', 'create']), Right now we only removed it. My question is, how to actually make this work so that I have a planRelation name in the View page?
3 Replies
Mina
MinaOP10mo ago
Here's the entire form: return $form ->schema([ Forms\Components\Select::make('user_id')->label('Created By')->relationship('userRelation', 'name'), Forms\Components\TextInput::make('name')->label('Shop Domain')->rules(['required', 'max:255'])->required(), Forms\Components\TextInput::make('planRelation.name') ->label('Plan') ->formatStateUsing((function (Whitelist $record): string { return $record->planRelation->name ?? ''; })) ->hiddenOn(['edit', 'create']), Forms\Components\Select::make('plan_id')->label('Plan')->options(function () { return array_filter(Plan::where('requires_whitelisting', true)->pluck('name', 'id')->toArray()); })->required()->hiddenOn(['view']), Forms\Components\Checkbox::make('send_email')->label('Send Email')->hiddenOn(['view']), Forms\Components\ViewField::make('install_link')->label('Install Link') ->view('filament.tables.columns.whitelist.install-link')->hiddenOn(['edit', 'create']), Forms\Components\TextInput::make('email')->label('Merchant Email')->required()->rules(['email', 'max:255']), ]);
toeknee
toeknee10mo ago
return $form
->schema([
Forms\Components\Select::make('user_id')->label('Created By')->relationship('userRelation', 'name'),
Forms\Components\TextInput::make('name')->label('Shop Domain')->rules(['required', 'max:255'])->required(),
Forms\Components\TextInput::make('planRelation.name')
->label('Plan')
->formatStateUsing((function (Whitelist|null $record): string {
return $record && $record->planRelation->name ? $record->planRelation->name : '';
}))
->hiddenOn(['edit', 'create']),
Forms\Components\Select::make('plan_id')->label('Plan')->options(function () {
return array_filter(Plan::where('requires_whitelisting', true)->pluck('name', 'id')->toArray());
})->required()->hiddenOn(['view']),
Forms\Components\Checkbox::make('send_email')->label('Send Email')->hiddenOn(['view']),
Forms\Components\ViewField::make('install_link')->label('Install Link')
->view('filament.tables.columns.whitelist.install-link')->hiddenOn(['edit', 'create']),
Forms\Components\TextInput::make('email')->label('Merchant Email')->required()->rules(['email', 'max:255']),

]);
return $form
->schema([
Forms\Components\Select::make('user_id')->label('Created By')->relationship('userRelation', 'name'),
Forms\Components\TextInput::make('name')->label('Shop Domain')->rules(['required', 'max:255'])->required(),
Forms\Components\TextInput::make('planRelation.name')
->label('Plan')
->formatStateUsing((function (Whitelist|null $record): string {
return $record && $record->planRelation->name ? $record->planRelation->name : '';
}))
->hiddenOn(['edit', 'create']),
Forms\Components\Select::make('plan_id')->label('Plan')->options(function () {
return array_filter(Plan::where('requires_whitelisting', true)->pluck('name', 'id')->toArray());
})->required()->hiddenOn(['view']),
Forms\Components\Checkbox::make('send_email')->label('Send Email')->hiddenOn(['view']),
Forms\Components\ViewField::make('install_link')->label('Install Link')
->view('filament.tables.columns.whitelist.install-link')->hiddenOn(['edit', 'create']),
Forms\Components\TextInput::make('email')->label('Merchant Email')->required()->rules(['email', 'max:255']),

]);
try the above
Mina
MinaOP10mo ago
I've tried it and I think it works, I am just waiting for the confirmation from my colleague
Want results from more Discord servers?
Add your server