【问题标题】:Codeigniter. Validate PUT data代码点火器。验证 PUT 数据
【发布时间】:2011-10-25 09:03:32
【问题描述】:

我正在使用 Phils RestServer 开发一个 API,我需要验证传入的 PUT 数据。它适用于 传入 POST 数据,但不是 PUT。

如何验证通过 PUT 发送的数据?

感谢所有输入!

【问题讨论】:

  • 看这里:php.net/manual/en/features.file-upload.put-method.php 示例 1 显示了一种读取 PUT 数据的方法,因此只需像处理任何其他文件一样摆脱文件写入和验证。
  • 嗯,我正在使用 Codeigniter,它有一个内置的验证系统。它适用于 POST 请求,但不适用于 PUT。如何让它与 PUT 一起使用?
  • 不幸的是,我认为这是对 CodeIgniter 的相当高级的修改。您可能会更好地逐案验证。尤其如此,因为您可能有(一个)特定的控制器来处理 PUT 请求。
  • 在版本 3 中,您可以将表单验证应用于任何数组,而不仅仅是 $_POST。像这样: $this->form_validation->set_data($data); // 其中 $data 将是您通过 PUT 获得的数据。这里的文档:codeigniter.com/userguide3/libraries/…

标签: codeigniter validation


【解决方案1】:

不!不工作。你能做的就是破解它,所以:

$_POST['foo'] = $this->put('foo');

允许 CodeIgniter 表单验证类验证除 POST 之外的其他内容在待办事项列表中(正是出于这个原因)。

【讨论】:

  • 在你的控制器中运行之前()
  • 我很抱歉问,但具体在哪里。在构造函数中还是在函数中?
  • DELETE也有问题。我不应该能够做 method.json?foo=bar 并能够使用 $this->delete('foo'); 访问 foo 吗?我不工作。仅当我在请求正文中发送它时。这也不起作用 $this->get('foo');
  • 很明显,因为 DELETE 不是 POST。在一个完美的“规范跟随世界”中,删除无论如何都不会发送参数,只需调用一个 URL 来删除。
【解决方案2】:

这是我的解决方案。我正在使用表单验证库中的 set_data() 函数:

$set_data = array(
        'username'  => $this->put('username'),
        'email'     => $this->put('email'),
        'zipcode'   => $this->put('zipcode'),
        'telephone' => $this->put('telephone'),
        'password'  => $this->put('password'),
    );

    $this->form_validation->set_data($set_data);
    $this->form_validation->set_rules($this->Register_model->rules_register);

【讨论】:

    【解决方案3】:

    我第一次回答后意识到这一切都取决于您使用的是什么版本的 CI,如果您使用的是最新的 3.0.0 或更高版本,那么您可以在表单验证文件中进行 1 个小调整。

    176 行 set_rules() 方法顶部的 Form_validation.php 文件

    来自:

    if ($this->CI->input->method() !== 'post' && empty($this->validation_data)) {
        return $this;
    }
    

    到:

    if ($this->CI->input->method() !== 'post' && $this->CI->input->method() !== 'put' && empty($this->validation_data)) {
        return $this;
    }
    

    如果这有帮助,请告诉我。

    【讨论】:

      【解决方案4】:

      使用 set_data() 可以正常工作。假设 'MRP' 和 'IsActive' 参数是通过 put 请求发送的。

      $this->form_validation->set_data($this->put());
      
      $this->form_validation->set_rules('MRP', 'MRP', 'numeric|greater_than[0]|less_than[10000]', array());
      $this->form_validation->set_rules('IsActive', 'Is Active', 'required|alpha|min_length[2]|max_length[2]', array());
      
      if ($this->form_validation->run() === TRUE) {
          // Do Stuff..
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多