Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Sanitize or manipulate request input before validation

License

Notifications You must be signed in to change notification settings

adamthehutt/laravel-sanitized-requests

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Sanitized Requests

Sanitize request input before passing to validation.

Installation

composer require adamthehutt/laravel-sanitized-requests

Usage

Create a Request class and use the SanitizesInput trait. Then implement the sanitize(Sanitizer $sanitizer) method to clean up user input before it is validated.

use AdamTheHutt\SanitizedRequests\SanitizesInput;
use AdamTheHutt\SanitizedRequests\Sanitizer;
use Illuminate\Foundation\Http\FormRequest;

class StoreItem extends FormRequest
{
    use SanitizesInput;

    public function sanitize(Sanitizer $sanitizer): void
    {
        $sanitizer->castToInt("user_id")
                  ->castToBoolean("for_sale")
                  ->castToFloat("price")
                  ->forgetIfEmpty("item_description")
                  ->trimWhitespace("nested.*.wildcard.param");
    }
}

Sanitizers

The following sanitize methods are currently supported:

  • castToBool($key) - Casts the string input value to a boolean
  • castToInt($key) - Casts the string input value to an integer
  • castToFloat($key) - Casts the string input value to a float
  • castToString($key) - Casts the input value to a string
  • forgetIfEmpty($key) - Removes from input both empty strings and arrays with only empty values
  • map($key, $callable) - Applies an arbitrary callback to the input value(s)
  • trimWhitespace($key) - Trims whitespace from a string input value

About

Sanitize or manipulate request input before validation

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages