#How to make filament to trim form input?

6 messages · Page 1 of 1 (latest)

sudden wren
#

Laravel uses TrimStrings middleware to automatically trim all form string inputs. However, I noticed that filament allows data to be saved with trailing spaces. How do I enable filament to trim form inputs?

white bison
#
// its by design from Livewire to skip TrimStrings
// in the source code here
  vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php
// in the boot method they have
$this->skipRequestPayloadTamperingMiddleware();

// which does exactly this
    function skipRequestPayloadTamperingMiddleware()
    {
        .....        
        // as you can see it skip it
        \Illuminate\Foundation\Http\Middleware\TrimStrings::skipWhen(function () {
            return $this->isLivewireRequest();
        });
    }
#

You will have to handle it outside Filament/Livewire, for example you can use the method configureUsing on the field for example

#
TextInput::configureUsing(function (TextInput $input) {
            $input->mutateDehydratedStateUsing(function ($state) {
                return Str::trim($state);
            });
        });
#

Which you use in your AppServiceProvider boot method

#

and same applies to the rest of the fields like TextArea etc hope that helps