39

GitHub - ArondeParon/laravel-request-sanitizer: An easy to use request sanitizer...

 5 years ago
source link: https://github.com/ArondeParon/laravel-request-sanitizer
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

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

$ phpunit

Credits

License

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK