Download the PHP package jcergolj/request-input-transformer-for-laravel without Composer
On this page you can find all versions of the php package jcergolj/request-input-transformer-for-laravel. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jcergolj/request-input-transformer-for-laravel
More information about jcergolj/request-input-transformer-for-laravel
Files in jcergolj/request-input-transformer-for-laravel
Package request-input-transformer-for-laravel
Short Description A Laravel package for transforming request inputs
License MIT
Informations about the package request-input-transformer-for-laravel
Request Input Transformer for Laravel
Clean up your prepareForValidation()
— the right way.
Why?
If you’ve built more than a couple of Laravel apps, you’ve probably ended up stuffing too much logic into prepareForValidation()
.
Maybe you’ve written code like this:
We've all been there — a quick trim()
here, a lower()
there. But soon, prepareForValidation()
is packed with string cleanup, formatting, and conditionals.
This package solves this.
Installation
Under the Hood: Pipeline Pattern
This package uses Laravel’s Pipeline Pattern under the hood.
That means your modifiers and transformers are run one by one, just like middleware — each one gets the current value (or request), does its thing, and passes it to the next.
Modifiers vs Transformers
This package gives you two ways to work with request input: modifiers and transformers.
🛠 Modifiers
Modifiers change values of existing fields in the request — great for cleanup and normalization.
Use modifiers when the field already exists and you need to modify only that field .
Modifiers merges fields automatically inside pipeline then
method.
🔄 Transformers
Transformers have access to the entire request and can add or change fields based on logic, relationships, or conditions.
Use transformers when you need to generate new fields or apply logic that involves more than one input.
Nested Input Support
This package supports nested input keys — including wildcard syntax — out of the box. You can apply modifiers or transformers to fields like:
Usage
Step 1: Create a Modifier and Transformer
Modifier: Trim strings
Transformer: Generate a full name from first and last name
Step 2: Apply in a FormRequest
Generating Modifiers and Transformers
To quickly create modifier or transformer classes, you can use the included Artisan commands:
Testing
What about testing modifiers and transformers?
One of the challenges with prepareForValidation()
is that Laravel doesn't offer a clean way to test if it actually ran — especially when you're trying to do as much on a unit level as possible.
I initially tried building custom PHPUnit assertions that would inspect the applied modifiers and transformers, but it quickly became complicated.
My current workaround
A practical alternative is to expose two public methods inside your FormRequest
class — one returning modifiers, the other transformers — and then test those separately.
Example:
Now in your test, you can do something like this:
This doesn’t guarantee that RequestPreprocessor was actually called inside prepareForValidation() — but it does confirm the configuration.
Mocking the RequestPreprocessor in Tests
Something that I haven't tried but it might actually work is using reflection for prepareForValidation
and to mock RequestPreprocessorFacade
.
Something like this.
Testing
To run the tests:
License
The MIT License (MIT).