ArondeParon / laravel-request-sanitizer by ArondeParon

An easy to use request sanitizer that allows you to sanitize your form data before validating it.
114,656
111
2
Package Data
Maintainer Username: ArondeParon
Maintainer Contact: hi@aron.codes (Aron Rotteveel)
Package Create Date: 2018-08-17
Package Last Update: 2024-06-16
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-11-08 03:21:29
Package Statistics
Total Downloads: 114,656
Monthly Downloads: 1,809
Daily Downloads: 101
Total Stars: 111
Total Watchers: 2
Total Forks: 7
Total Open Issues: 0

Laravel Request Sanitizer

Build Status

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

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.

How to use

Syntax is similar to the way rules are added to a Form Request.

class StoreCustomerInformationRequest extends FormRequest
{
     use SanitizesInputs;
     
     protected $sanitizers = [
        'lastname' => [
            Capitalize::class,
        ],
        'mobile_phone' => [
            RemoveNonNumeric::class
        ],
     ];
}

Installing

composer require arondeparon/laravel-request-sanitizer

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.

Predefined Sanitizers

  • Trim - simple PHP trim() implementation
  • TrimDuplicateSpaces replaces duplicate spaces with a single space.
  • RemoveNonNumeric - removes any non numeric character
  • Capitalize - capitalizes the first character of a string
  • Uppercase - converts a string to uppercase
  • Lowercase - converts a string to lowercasse
  • Contributions are appreciated!

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

$ composer test

Credits

License

The MIT License (MIT). Please see License File for more information.