Form trying to update a field that is not in the form

This is my form (simplified)
Textarea::make('description'),
Toggle::make('is_active'),
Textarea::make('description'),
Toggle::make('is_active'),
When I try to save it, the query also includes a field that is not in the form
update
`some_table`
set
`description` = ?,
`is_active` = 1,
`alert` = 0, <-???
where
`id` = 2
update
`some_table`
set
`description` = ?,
`is_active` = 1,
`alert` = 0, <-???
where
`id` = 2
As you can see, there is an extra alert field there. Just to be clear, the tables does have an alert column, but why is the form trying to update it if it's not in the schema?
12 Replies
LeandroFerreira
share the whole code
ChesterS
ChesterSOP3w ago
There's not much to share TBH. Here is the schema
public static function form(Form $form): Form
{
return $form
->schema([
Textarea::make('description'),
Toggle::make('is_active'),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Textarea::make('description'),
Toggle::make('is_active'),
]);
}
But I think the issue might be related to the alert field. It's included through a trait like this
trait HasAlert
{
public function initializeHasAlert()
{
$this->mergeCasts([
'alert' => Alert::class,
]);

$this->mergeFillable([
'alert',
]);
}
}
trait HasAlert
{
public function initializeHasAlert()
{
$this->mergeCasts([
'alert' => Alert::class,
]);

$this->mergeFillable([
'alert',
]);
}
}
I still don't understand why it would try to save it if it's not in the form or if there's a workaround for it
LeandroFerreira
where is the alert?
ChesterS
ChesterSOP3w ago
What do you mean?
LeandroFerreira
where are you using this alert?
ChesterS
ChesterSOP3w ago
I'm ... still not sure what you mean? :/ I don't use it anywhere in the form - it's a field that exists on the model I just created a trait for it because I also use the same login logic in other models
LeandroFerreira
hum.. CreatePage.php
protected function beforeCreate(): void
{
dd($this->data);
}
protected function beforeCreate(): void
{
dd($this->data);
}
what is the output?
ChesterS
ChesterSOP3w ago
Hmm this is an edit form
LeandroFerreira
beforeSave
ChesterS
ChesterSOP3w ago
Yeap I'll try both sec I tried these
protected function mutateFormDataBeforeSave(array $data): array
{
dd($data);
}

protected function beforeSave(): void
{
dd($this->data);
}
protected function mutateFormDataBeforeSave(array $data): array
{
dd($data);
}

protected function beforeSave(): void
{
dd($this->data);
}
but nothing happens. Is it because the form is in the resource?
LeandroFerreira
did you add this to the EditPage?
ChesterS
ChesterSOP3w ago
Yeap
class EditProperty extends EditRecord
{
protected static string $resource = PropertyResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}

protected function mutateFormDataBeforeSave(array $data): array
{
dd($data);
}

protected function beforeSave(): void
{
dd('save', $this->data);
}
}
class EditProperty extends EditRecord
{
protected static string $resource = PropertyResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}

protected function mutateFormDataBeforeSave(array $data): array
{
dd($data);
}

protected function beforeSave(): void
{
dd('save', $this->data);
}
}
actually nvm...Here is the data
"id" => 1
"uuid" => "95de666b-2d6a-45e5-a1a0-71903007af0e"
"description" => "asdfasdf"
"is_managed" => true
"deleted_at" => null
"created_at" => "2024-03-08T10:33:59.000000Z"
"updated_at" => "2024-12-05T19:42:43.000000Z"
"alert" => null
]
"id" => 1
"uuid" => "95de666b-2d6a-45e5-a1a0-71903007af0e"
"description" => "asdfasdf"
"is_managed" => true
"deleted_at" => null
"created_at" => "2024-03-08T10:33:59.000000Z"
"updated_at" => "2024-12-05T19:42:43.000000Z"
"alert" => null
]
Ok I fixed it by changing the cast attribute. I thought Filament only tries to update fields that you set in the form :/ Thank you for your time 🙇‍♂️
Want results from more Discord servers?
Add your server