PHPackages                             arondeparon/laravel-request-sanitizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. arondeparon/laravel-request-sanitizer

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

arondeparon/laravel-request-sanitizer
=====================================

An easy to use request sanitizer that allows you to sanitize your form data before validating it.

6.2.0(11mo ago)112151.6k↓16.8%71MITPHPPHP ^8.1CI failing

Since Aug 17Pushed 11mo ago2 watchersCompare

[ Source](https://github.com/arondeparon/laravel-request-sanitizer)[ Packagist](https://packagist.org/packages/arondeparon/laravel-request-sanitizer)[ Docs](https://github.com/arondeparon/laravel-request-sanitizer)[ GitHub Sponsors](https://github.com/arondeparon)[ RSS](/packages/arondeparon-laravel-request-sanitizer/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (24)Used By (1)

Laravel Request Sanitizer
=========================

[](#laravel-request-sanitizer)

[![Total Downloads](https://camo.githubusercontent.com/4cfbc5caf02bed030550554832bf26e6c2b13721992aaf36ceeaeadbb758714e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61726f6e64657061726f6e2f6c61726176656c2d726571756573742d73616e6974697a65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/arondeparon/laravel-request-sanitizer)

The `arondeparon/laravel-request-sanitizer` package provides a fluent interface to sanitize form requests before validating them.

Why should I use this package?
------------------------------

[](#why-should-i-use-this-package)

Often, validating your request is not enough. The request sanitizer allows you to easily manipulate your form data before passing it to the validator. You can start using it in a matter of minutes and it is fully compatible with Laravel's `FormRequest` object.

Table of Contents
-----------------

[](#table-of-contents)

- [How to use](#how-to-use)
- [Installing](#installing)
- [Usage](#usage)
    - [Basic Usage](#basic-usage)
    - [Using Wildcards](#using-wildcards)
- [Predefined Sanitizers](#predefined-sanitizers)
    - [FilterVars usage](#filtervars-usage)
- [Writing your own Sanitizer](#writing-your-own-sanitizer)
- [Testing](#testing)
- [Credits](#credits)
- [License](#license)

How to use
----------

[](#how-to-use)

Syntax is similar to the way `rules` are added to a [Form Request](https://laravel.com/docs/master/validation#form-request-validation).

```
class StoreCustomerInformationRequest extends FormRequest
{
     use SanitizesInputs;

     protected $sanitizers = [
        'lastname' => [
            Capitalize::class,
        ],
        'mobile_phone' => [
            RemoveNonNumeric::class
        ],
     ];
}
```

Installing
----------

[](#installing)

`composer require arondeparon/laravel-request-sanitizer`

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

- Add the `SanitizesInputs` trait to your form request.
- Write your own sanitizers or use one of the supplied sanitizers and add them to the `$sanitizers`property of your form request.
- Your request data will now be sanitized before being validated.

### Using Wildcards

[](#using-wildcards)

The sanitizer supports wildcard patterns in your form keys, allowing you to apply sanitizers to multiple fields that match a pattern. This is particularly useful when dealing with arrays or nested data structures.

```
class StoreUsersRequest extends FormRequest
{
    use SanitizesInputs;

    protected $sanitizers = [
        // Apply to all email fields in the users array
        'users.*.email' => [
            Lowercase::class,
            TrimDuplicateSpaces::class
        ],

        // Apply to all name fields in the users array
        'users.*.name' => [
            Capitalize::class
        ],

        // Multiple wildcards for deeply nested structures
        'departments.*.employees.*.email' => [
            Lowercase::class
        ]
    ];
}
```

Example input:

```
$request = [
    'users' => [
        ['email' => 'JOHN@EXAMPLE.COM', 'name' => 'john doe'],
        ['email' => 'JANE@EXAMPLE.COM', 'name' => 'jane smith']
    ],
    'departments' => [
        'sales' => [
            'employees' => [
                ['email' => 'SALES@EXAMPLE.COM'],
                ['email' => 'SUPPORT@EXAMPLE.COM']
            ]
        ]
    ]
];
```

After sanitization:

```
$sanitized = [
    'users' => [
        ['email' => 'john@example.com', 'name' => 'John Doe'],
        ['email' => 'jane@example.com', 'name' => 'Jane Smith']
    ],
    'departments' => [
        'sales' => [
            'employees' => [
                ['email' => 'sales@example.com'],
                ['email' => 'support@example.com']
            ]
        ]
    ]
];
```

The wildcard pattern (`*`) will match any single segment in the dot notation path. You can use multiple wildcards to match nested structures at any depth.

Predefined Sanitizers
---------------------

[](#predefined-sanitizers)

- [`Trim`](./src/Sanitizers/Trim.php) - simple PHP `trim()` implementation
- [`TrimDuplicateSpaces`](./src/Sanitizers/TrimDuplicateSpaces.php) replaces duplicate spaces with a single space.
- [`RemoveNonNumeric`](./src/Sanitizers/RemoveNonNumeric.php) - removes any non numeric character
- [`Capitalize`](./src/Sanitizers/Capitalize.php) - capitalizes the first character of a string
- [`Uppercase`](./src/Sanitizers/Uppercase.php) - converts a string to uppercase
- [`Lowercase`](./src/Sanitizers/Lowercase.php) - converts a string to lowercase
- [`FilterVars`](./src/Sanitizers/FilterVars.php) - simple PHP `filter_var` sanitizer
- [`CarbonDate`](./src/Sanitizers/CarbonDate.php) - cast a string to a Carbon object
- Contributions are appreciated!

### FilterVars usage

[](#filtervars-usage)

The FilterVars sanitizer acts as a wrapper of the default PHP `filter_var` function. It accepts the same (optional) parameters as the original function. Both parameters can be either an `array` or `string` type:

```
 {
    protected $sanitizers = [
        'last_name' => [
            FilterVars::class => [
                'filter' => FILTER_SANITIZE_STRING,
                'options' => FILTER_FLAG_STRIP_BACKTICK
            ]
        ]
    ];
 }
```

For more information on filter\_vars please refer to .

Writing your own Sanitizer
--------------------------

[](#writing-your-own-sanitizer)

Writing your own sanitizer can be done by implementing the `Sanitizer` interface, which requires only one method.

```
interface Sanitizer
 {
     public function sanitize($input);
 }
```

Testing
-------

[](#testing)

`$ composer test`

Credits
-------

[](#credits)

- [Aron Rotteveel](https://github.com/arondeparon)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance51

Moderate activity, may be stable

Popularity47

Moderate usage in the ecosystem

Community21

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 80.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~118 days

Recently: every ~318 days

Total

22

Last Release

341d ago

Major Versions

1.1.1 → 2.02019-11-08

2.3 → 3.02020-09-09

3.1.1 → 4.02021-09-08

4.0.2 → 5.0.02022-02-28

5.0.0 → 6.0.02024-06-16

PHP version history (5 changes)0.1.0PHP ^7.0

2.0PHP ^7.2

3.0PHP ^7.3

3.1.0PHP ^7.3|^8.0

6.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/95f389d71b88d1dcfafbe056d502bf8353a2a5e41b7106a9dc1e475f964a38f8?d=identicon)[ArondeParon](/maintainers/ArondeParon)

---

Top Contributors

[![arondeparon](https://avatars.githubusercontent.com/u/7697?v=4)](https://github.com/arondeparon "arondeparon (82 commits)")[![gitmiro](https://avatars.githubusercontent.com/u/3705700?v=4)](https://github.com/gitmiro "gitmiro (7 commits)")[![erickmcarvalho](https://avatars.githubusercontent.com/u/7072721?v=4)](https://github.com/erickmcarvalho "erickmcarvalho (4 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (4 commits)")[![johnpaulmedina](https://avatars.githubusercontent.com/u/1139439?v=4)](https://github.com/johnpaulmedina "johnpaulmedina (1 commits)")[![rjvandoesburg](https://avatars.githubusercontent.com/u/1488300?v=4)](https://github.com/rjvandoesburg "rjvandoesburg (1 commits)")[![samnela](https://avatars.githubusercontent.com/u/1852108?v=4)](https://github.com/samnela "samnela (1 commits)")[![jaulz](https://avatars.githubusercontent.com/u/5358638?v=4)](https://github.com/jaulz "jaulz (1 commits)")[![dependabot-support](https://avatars.githubusercontent.com/u/112581971?v=4)](https://github.com/dependabot-support "dependabot-support (1 commits)")

---

Tags

hacktoberfestlaravelphprequestsanitizerrequestlaravelvalidationsanitizerformarondeparon

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/arondeparon-laravel-request-sanitizer/health.svg)

```
[![Health](https://phpackages.com/badges/arondeparon-laravel-request-sanitizer/health.svg)](https://phpackages.com/packages/arondeparon-laravel-request-sanitizer)
```

###  Alternatives

[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[laravel-lang/attributes

Translation of form element names

273.8M11](/packages/laravel-lang-attributes)[timacdonald/rule-builder

A fluent rule builder for Laravel validation rule generation.

1027.7k](/packages/timacdonald-rule-builder)[lrgt/laravel-form-ajax-validation

Make ajax validation with Laravel Requests for forms with bootstrap

435.6k](/packages/lrgt-laravel-form-ajax-validation)[laravel-validation-rules/ip

Validate if an ip address is public or private.

1629.7k](/packages/laravel-validation-rules-ip)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
