Laravel 5 Validation Trim

2019-04-09 18:59发布

问题:

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.

回答1:

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


回答2:

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


回答3:

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



回答4:

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


回答5:

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



回答6:

$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


标签: php laravel trim