Laravel 5 Validation Trim

后端 未结 8 921
梦谈多话
梦谈多话 2020-12-18 05:23

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

相关标签:
8条回答
  • 2020-12-18 05:47

    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

    0 讨论(0)
  • 2020-12-18 05:49

    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()));
    
    0 讨论(0)
  • 2020-12-18 05:52

    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(...);
    
    0 讨论(0)
  • 2020-12-18 05:54
    public function formatInput()
    {
      $input = array_map('trim', $this->all());
      $this->replace($input);
      return $this->all();
    }
    
    0 讨论(0)
  • 2020-12-18 05:58

    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

    0 讨论(0)
  • 2020-12-18 06:01

    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

    0 讨论(0)
提交回复
热议问题