F
Filament14mo ago
Damien

Mutating form data when using simple resources and wizards

I have generated a simple resource from following along in the documentation and I have created a wizard to enter a new entity into my database. One of the values however, just comes from the user creating the record but I cannot figure out which file (Contact/ContactResource/ManageContacts) where I am supposed to make the mutation that the documentation suggestions. A piece of constructive feedback from myself would be, although there are a lot of code examples in the docs, they don't generally say where they live / which file they should be in which for me, creates confusion slightly. Apologies if I am missing something obvious here, first time using laravel and straight in with Filament,
Solution:
That should work, are you sure you have created_by_id in the fillables of the model?
Jump to solution
10 Replies
toeknee
toeknee14mo ago
This would be on the action it's self so where you see CreateAction::make you would chain it on here It's not ideal jumping into filament wihtout knowing Laravel or Livewire.
Damien
DamienOP14mo ago
Sorry I dropped a message and ran off for an appointment. sadly, this is a work circumstance and I don't have much choice. upskilling in all areas very quickly. So at the moment I have this:
class ManageContacts extends ManageRecords
{
protected static string $resource = ContactResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()->modalWidth('lg')->createAnother(false)
];
}
}
class ManageContacts extends ManageRecords
{
protected static string $resource = ContactResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()->modalWidth('lg')->createAnother(false)
];
}
}
If I chain on mutateFormDataUsing here, it doesn't work and I suspect it is the wrong place to do so. Would I need to create a new method here?
toeknee
toeknee14mo ago
That is the correc place, can you provide code
Damien
DamienOP14mo ago
Sure, the entire file looks like this:
<?php

namespace App\Filament\Resources\ContactResource\Pages;

use App\Filament\Resources\ContactResource;
use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;

class ManageContacts extends ManageRecords
{
protected static string $resource = ContactResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->modalWidth('lg')
->createAnother(false)
->mutateFormDataUsing(function (array $data): array {
$data['created_by_id'] = auth()->id();

return $data;
}),
];
}
}
<?php

namespace App\Filament\Resources\ContactResource\Pages;

use App\Filament\Resources\ContactResource;
use Filament\Actions;
use Filament\Resources\Pages\ManageRecords;

class ManageContacts extends ManageRecords
{
protected static string $resource = ContactResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make()
->modalWidth('lg')
->createAnother(false)
->mutateFormDataUsing(function (array $data): array {
$data['created_by_id'] = auth()->id();

return $data;
}),
];
}
}
However when I try to create a new contact, it complains that created_by_id has no default value (is not being provided). Here is the form too in case it helps:
public static function form(Form $form): Form
{
// ... contact

$billing = Wizard\Step::make('Billing')->schema([
Forms\Components\TextInput::make('addr_line_1')
->required()
->maxLength(255),
Forms\Components\TextInput::make('addr_line_2')
->maxLength(255),
Forms\Components\TextInput::make('city')
->required()
->maxLength(255),
Forms\Components\TextInput::make('postcode')
->required()
->maxLength(255),
Forms\Components\DateTimePicker::make('received_at')
->required(),
]);

//... admin

return $form->schema([
Wizard::make(['Contact', 'Billing'])
->steps([$contact, $admin, $billing])
])->columns(1);
}
public static function form(Form $form): Form
{
// ... contact

$billing = Wizard\Step::make('Billing')->schema([
Forms\Components\TextInput::make('addr_line_1')
->required()
->maxLength(255),
Forms\Components\TextInput::make('addr_line_2')
->maxLength(255),
Forms\Components\TextInput::make('city')
->required()
->maxLength(255),
Forms\Components\TextInput::make('postcode')
->required()
->maxLength(255),
Forms\Components\DateTimePicker::make('received_at')
->required(),
]);

//... admin

return $form->schema([
Wizard::make(['Contact', 'Billing'])
->steps([$contact, $admin, $billing])
])->columns(1);
}
Solution
toeknee
toeknee14mo ago
That should work, are you sure you have created_by_id in the fillables of the model?
toeknee
toeknee14mo ago
Maybe try moving mutateFormDataUsing to the top instead createAnother prevents moving
Damien
DamienOP14mo ago
ah that may be it! I didn't have it in the fillable array as I thought that could mean a user might be able to try and change it's value maliciously, let me give that a try however. Failing that, I will change the chain order.
toeknee
toeknee14mo ago
If it's not in the fillable it will be it. All values will be removed from the request that are not part of the form anyway
Damien
DamienOP14mo ago
thank you! I will give that a go, currently just fixing something else and then I will be right back to it. worked a treat thank you.
Want results from more Discord servers?
Add your server