Laravel 5 Validation Trim

You can use the following code to trim all string input (as you might have arrays in the input)

    // trim all input
    Input::merge(array_map(function ($value) {
        if (is_string($value)) {
            return trim($value);
        } else {
            return $value;
        }
    }, Input::all()));

It's not job for validator to change any input data. Trim validator exists in CodeIgniter, but as to me this isn't right place to perform trim.

You can automatically trim all input by using using this:

Input::merge(array_map('trim', Input::all()));

Now do the rest of your coding:

$username = Input::get('username'); // it's trimed 
// ...
Validator::make(...);

Due to the documention laravel HTTP Request by default laravel trim all the requests data.


and Trim the request in validation part is so dirty job. You could manage this feature like trim or convert empty string to null with the middleware

because middleware execute before the validation and you could have clean data in validation

Tags:

Php

Trim

Laravel