【问题标题】:Laravel 5 Validation TrimLaravel 5 验证修剪
【发布时间】:2015-06-05 19:54:03
【问题描述】:

我是 Laravel 5 的初学者。

如何删除验证器中的空格?我已阅读文档,但没有用于修剪的验证器(删除空格)。

这是我的规则

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

感谢您的回答。

【问题讨论】:

    标签: php laravel trim


    【解决方案1】:

    由于文档laravel HTTP Request 默认情况下,laravel 修剪所有请求数据。


    Trim 验证部分的请求是如此肮脏的工作。 您可以使用middleware 管理此功能,例如trimconvert empty string to null

    因为中间件在验证之前执行,您可以在验证中获得干净的数据

    【讨论】:

      【解决方案2】:

      您可以在请求文件中使用它。

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

      您还可以在请求文件中创建all() 函数来更新请求参数:

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

      【讨论】:

        【解决方案3】:
        $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
        

        【讨论】:

          【解决方案4】:

          我扩展了FormRequest 类并覆盖了在验证发生之前调用的prepareForValidation 方法。

          // 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);
          }
          

          它的作用:

          1. 将请求替换为具有修剪输入的新请求
          2. untrimmable 属性中设置我不想修剪的所有字段。
          3. 使用点表示法处理嵌套输入。

          这是指向要点的链接https://gist.github.com/msbrime/336a788c7cced2137bdc7896c1241239

          【讨论】:

          • 您好,能否请您不要只提供源链接,而是在答案中突出显示有用的部分。 PS:我编辑了你的答案,请尝试格式化你的答案——尤其是代码
          • 确定@Arount
          【解决方案5】:

          在 Laravel 5.2 或 5.3 中,您可以使用 trim 像这样删除空格

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

          因此这将删除所有已发布的空格表单输入,并且验证将正常工作

          【讨论】:

            【解决方案6】:
            public function formatInput()
            {
              $input = array_map('trim', $this->all());
              $this->replace($input);
              return $this->all();
            }
            

            【讨论】:

              【解决方案7】:

              您可以使用以下代码修剪所有字符串输入(因为您可能在输入中有数组)

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

              【讨论】:

              • 或者你可以顺便使用三元运算符return is_string($value) ? trim($value) : $value;,op应该选择这个答案。
              【解决方案8】:

              验证器不负责更改任何输入数据。 CodeIgniter 中存在 Trim 验证器,但对我而言,这不是执行 trim 的正确位置。

              您可以使用以下方法自动修剪所有输入:

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

              现在完成剩下的编码工作:

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

              【讨论】:

              • 这不支持数组。 @Alex 的回答更好。
              • 你应该这样做,但这不是你应该使用中间件的当前方式
              猜你喜欢
              • 1970-01-01
              • 2017-07-27
              • 1970-01-01
              • 2015-07-02
              • 2016-10-13
              • 1970-01-01
              • 2014-04-19
              • 2015-08-17
              • 1970-01-01
              相关资源
              最近更新 更多