fajar ainul
fajar ainul

Reputation: 540

Laravel 5 Validation Trim

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.

Upvotes: 8

Views: 33535

Answers (8)

Farshad Fahimi
Farshad Fahimi

Reputation: 648

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

Upvotes: 5

PHP Worm...
PHP Worm...

Reputation: 4224

You can use this in your request file.

$input = request()->all();
$input['url'] = trim($input['url']);
request()->replace($input);

You can also create an all() function in your request file to update request parameters:

 public function all()
    {
        $all = parent::all(); // $this->all();
        $all['new_key'] = "new_data";
        $all['old_key'] = "new_data";

        return $all;
    }

Upvotes: 2

Deep Gandhi
Deep Gandhi

Reputation: 79

$data = $r->all();
foreach ($data as $key => $value) {
if($value!=""){ //If your primaryKey id is autoincrement
   $data[$key] = preg_replace('/\s{2,}/',' ',$value);
}
}
$variable = new modelName($data);
$variable->save();

//Here Parameter Explaination
 => '/\s{2,}/' Pattern must write between '/ /' and \s{2,} means 2 or more than 2 spaces sholud be trim
 => ' ' second parameter is with. Means 1st parameter replace with 2nd parameter(here, one space)
 => $value is variable which is trim

Upvotes: 0

msbrime
msbrime

Reputation: 1

I extended the FormRequest class and overrode the prepareForValidation method which is called before validation happens.

// anything I don't want trimmed here
protected $untrimmable = [];

// replace the request with trimmed request here
protected function prepareForValidation()
{
    return $this->replace($this->trimData($this->all()));
}

// recursively trim the fields in the request here
protected function trimData($data,$keyPrefix = '')
{
    $trimmedFields = array_map(function($value,$field) use ($keyPrefix){
        // if the value is an array handle it as
        // a request array and send along the prefix
        if(is_array($value)){
            return $this->trimData($value,$this->dotIndex($keyPrefix,$field));
        }

        // if the field is not in the specified fields to be
        // left untrimmed
        if(
            !in_array($this->dotIndex($keyPrefix,$field),$this->dontTrim) && 
            !in_array($this->dotIndex($keyPrefix,$field), $this->untrimmable)
        ) {
            return trim((string) $value);
        }

        return $value;

    }, $data,array_keys($data));

    return array_combine(array_keys($data),$trimmedFields);
}

What it does:

  1. Replace request with a new one with trimmed inputs
  2. Set all fields I don't want trimmed in an untrimmable property.
  3. Handles nested inputs with dot notation .

Here's a link to the gist https://gist.github.com/msbrime/336a788c7cced2137bdc7896c1241239

Upvotes: 0

Khan Muhammad
Khan Muhammad

Reputation: 420

In Laravel 5.2 or 5.3 you can use trim for spaces remove like this

$input = array_map('trim', $request->all());

so this will remove all space form inputs that posted and validation will work fine

Upvotes: 3

afzylive
afzylive

Reputation: 21

public function formatInput()
{
  $input = array_map('trim', $this->all());
  $this->replace($input);
  return $this->all();
}

Upvotes: 1

Alex
Alex

Reputation: 34978

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()));

Upvotes: 13

Limon Monte
Limon Monte

Reputation: 54389

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(...);

Upvotes: 9

Related Questions