Giter Club home page Giter Club logo

laravel-request-sanitizer's Introduction

Laravel Request Sanitizer

Total Downloads

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.

Table of Contents

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

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 https://www.php.net/manual/en/function.filter-var.php.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.