Courses

Filament 3 From Scratch: Practical Course

Money Column: Modify Before/After Form

Summary of this lesson:
- Formatting display values in tables
- Mutating form data before save/load
- Handling data transformations in forms

In this lesson, I will show you how to customize the behavior with the field value, both in the form and table, with an example of the Money field.

We have a products.price DB field, which requires extra logic. According to all the DB theories, we shouldn't store monetary values as floats. Instead, we should use an integer field and store the value in cents.

It means that after someone enters 49.99 in the form, we need to automatically multiply it by 100 and save 4999 in the DB.

It also means that, from the other side, in the table, we need to take the 4999 value from the DB and automatically divide it by 100 to show 49.99 in the table column.

Let's see how to do both in Filament.


Mutate Value Before Creating

Filament has methods to modify the value between entering it in the form and saving it in the DB.

  • Method mutateFormDataBeforeCreate() for Create forms
  • Method mutateFormDataBeforeSave() for Edit forms

They both accept the array of field values, and we can modify that array however we want and return it from the function.

app/Filament/Resources/ProductResource/Pages/CreateProduct.php:

class CreateProduct extends CreateRecord
{
// ...
 
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['price'] = $data['price'] * 100;
 
return $data;
}
}

Now, if we enter 123.45 in the create form field, it will show 12345 both in the database and in the table:

Let's fix that table column value.


Format Value in Table Column

We will add two modifier methods: dividing the value by 100 and formating the value with a currency.

For re-calculating the value from DB, you can use the ->getStateUsing() method, which acts similarly to the Eloquent Accessor.

For formatting the monetary value with separators, we can just add a method ->money() to the column. Not only that, we can specify which currency to show.

Tables\Columns\TextColumn::make('price')
->sortable()
->money('usd')
->getStateUsing(function (Product $record): float {
return $record->price / 100;
}),

Now, if I add a price of 12345.67 to a new product, it will look like this in the table:


Edit Form: Modify Value Before/After

Now, if we load the Edit form, we will have the same problem: the value will come exactly as it is in the DB: 1234567 instead of 12345.67.

And also, when saving, there won't be any modification either. So let's fix both with two methods:

app/Filament/Resources/ProductResource/Pages/EditProduct.php:

class EditProduct extends EditRecord
{
// ...
 
protected function mutateFormDataBeforeFill(array $data): array
{
$data['price'] = $data['price'] / 100;
 
return $data;
}
 
 
protected function mutateFormDataBeforeSave(array $data): array
{
$data['price'] = $data['price'] * 100;
 
return $data;
}
}

As you can see, in the beforeFill(), we divide by 100, and in the beforeSave(), we multiply back. Again, this behavior is similar to Eloquent Accessors, just in Filament syntax.

Now, our Edit form looks/works correctly!


With this lesson, I wanted to show you an example of how to modify fields before/after saving them and when showing them in the table.

Previous: Column Sort, Search and Validate
avatar

On edit, i have this error The name has already been taken. which would have to be changed in this line of code: TextInput::make('name')->required()->unique(),

avatar

Yeah, I noticed that myself, will fix later in the tutorial, meanwhile try this trick

avatar

->unique(ignorable: fn ($record) => $record)

avatar
You can use Markdown
avatar

If I create a new product, I like to accept a comma separated price as well as a dot separated one. This means I have to str_replace the price in order to replace any comma with a dot before inserting the price to the db. But since my validation contains a numeric check, the str_replace method must kick in before validation, otherwise it will fail if the user uses comma as a float separator. Any ideas how I can accomplsh that?

👍 1
avatar

I have seen people are doing that comma/dot automatic replacement in JavaScript on the front-end, before any back-end validation happening.

avatar

This worked for me:

TextInput::make('price')
  ->mask(RawJs::make(<<<'JS'
	$input = $input.replace(/,/g, '.')
JS)),
avatar
You can use Markdown
avatar

I'm Brazilian and we use a comma as a thousands separator. I followed the documentation suggestion at the link below and created a Cast class, which did all the work for me: https://filamentphp.com/docs/3.x/panels/getting-started#casting-the-price-to-an-integer

👍 4
avatar
You can use Markdown
avatar

Hello, I have a question: in a Filament Form I have a manyToMany relationship rendered with a Checkboxlist. Before the db saving, I have to perform some "actions" with the values inside the list, but If i go in the methods mutateFormDataBeforeSave or handleRecordCreation the $data variable contains all the form data except the relationship one.

Possible that there is no way to handle that values?

avatar

How are you definin chechbox list? Forms doesn't work with dot notation. Wrapping in a group and defining relationship there maybe would work.

avatar

like this: Forms\Components\CheckboxList::make('vagoni') ->label('Vagoni') ->relationship( 'wagons', 'nome', fn (Builder $query) => $query->where('motrice', '=', false), )

avatar

What do you mean with group?

avatar
You can use Markdown
avatar

I always use DECIMAL(8,2) for money fields in MySQL.

avatar
Wilko van der Ploeg

Pretty much what I do too, I only use DECIMAL(8,5) to prevent rounding differences with VAT.

avatar
You can use Markdown
avatar

It's nice that Filament includes the mutate methods, but they look a bit clumsy to me when you consider that the same thing can be done in native Laravel on the model with just this:

    public function price(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => $value / 100,
            set: fn ($value) => $value * 100
        );
    }

..and the added advantage of doing this would seem to be that it works on the fontend too, not just in Filament forms/tables.

Is there any benefit you can think of to using the two Filament murate methods (mutateFormDataBeforeFill and mutateFormDataBeforeSave) as opposed to doing it on the Laravel model?

avatar

The attribute getter and setter is fine if that's what you need to do - you don't need the mutateFormDataBeforeSave method for that. But for me, I needed to add a couple of field values that are based on the value of other fields in the form. in my case:

protected function mutateFormDataBeforeCreate(array $data): array
{
	$data['slug'] = Str::slug($data['name']);
	$data['created_by'] = auth()->id;
	return $data;
}
avatar

Ok, yes, I see what you mean, thank you. It looks like it could be pretty useful for doing a load of form-specific processing that you might not necessarily want to do every time you save the model. So it's good to know the option is there.

avatar
You can use Markdown
avatar

I'm trying to use money format (MoneyCast like the documentation) and it works, but in the form TextInput, i'm able to put N decimal numbers, but I want to fix it to 2, using comma as thousands separator and dot as decimal separator. There is a native way to do in Filament 3?

This is my code:

Forms\Components\TextInput::make('tuition_average_fund')
		->label(SegmentEnum::FUNDAMENTAL_I->getLabel())
		->numeric()
		->inputMode('decimal')
		->prefix('R$')
avatar

This could help you:

TextInput::make('price')
    ->required()
    ->mask(RawJs::make(<<<'JS'
        $money($input, '.', '', 2)
    JS))
    ->placeholder('19.99')
    ->formatStateUsing(fn ($state) => ! $state ? null : number_format($state / 100, 2))
    ->dehydrateStateUsing(fn ($state) => (int) ($state * 100)),

This will create a mask input for your field

avatar
You can use Markdown
avatar

In new version follow this: https://filamentphp.com/docs/3.x/panels/getting-started#casting-the-price-to-an-integer

avatar
You can use Markdown
avatar
You can use Markdown