Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Validation Trim

Tags:

php

trim

laravel

I am a beginner in Laravel 5.

How can I remove whitespaces in validator?? i have read the documentation but there is no validator for trim(remove whitespaces).

here my rules

$rules = [
        'name' =>'required',
        'email' => 'required|email',
        'address' => 'required',
        'phones' => 'required'
    ];

thanks for your answer.

like image 628
fajar ainul Avatar asked Apr 01 '15 08:04

fajar ainul


3 Answers

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()));
like image 171
Alex Avatar answered Sep 18 '22 07:09

Alex


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(...);
like image 36
Limon Monte Avatar answered Sep 19 '22 07:09

Limon Monte


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

like image 45
Farshad Fahimi Avatar answered Sep 19 '22 07:09

Farshad Fahimi