【问题标题】:CakePHP: "GET" form does not auto-populate form fields after submissionCakePHP:“GET”表单在提交后不会自动填充表单字段
【发布时间】:2013-04-01 20:44:31
【问题描述】:

我使用的是 Cake 2.3.0。如果我使用POST 提交我的表单,则选定的表单字段会继续,但是如果我使用GET 提交我的表单,所有表单字段都会返回到它们的默认值。

有没有办法让GET 提交像POST 一样工作?

这是我的控制器:

class ListingsController extends AppController {
    public function results() {
        $conditions = array(
            'Listing.Beds >=' => $this->request->query['beds'], 
            'Listing.ListingStatus >=' => $this->request->query['status'], 
        );

        $this->paginate = array(
            'conditions' => $conditions,
        );    

        $this->set('listings', $this->paginate());
    }
}

这是我的视图。

echo $this->Form->create(null, array(
    'controller' => 'listings', 
    'action' => 'results',
    'type' => 'get'
));

echo $this->Form->input('name');

$beds = array('1' => '1+', '2' => '2+', '3' => '3+', '4' => '4+', '5' => '5+');
echo $this->Form->input('beds', array('options' => $beds));

$status = array('Active' => 'Active', 'Pending' => 'Pending', 'ActivePending' => 'Active and Pending');
echo $this->Form->input('status', array('options' => $status));

echo $this->Form->end('Update'); 

所以基本上如果我将'type' => 'get' 更改为'type' => 'post' 它就可以正常工作。但我需要能够通过GET 做到这一点。

谢谢

【问题讨论】:

    标签: php forms cakephp query-string cakephp-2.3


    【解决方案1】:

    我同意这很烦人(IMO CakePHP 应该足够聪明,可以根据“类型”自动确定从“哪里”获取数据)。

    您必须将请求的“查询”复制到请求的“数据”;

    $this->request->data = $this->request->query;
    

    不在我的电脑后面测试它(像往常一样,哈哈),但应该可以工作。

    【讨论】:

    • 其实我已经试过了,但是没有用。我能够做的是遍历 $this->request->query 数组并将这些值传递给匹配的 $this->request->data。但正如在聊天室中向我指出的那样,这是“艰难的意大利面条式”,可能不应该那样做。
    • 显然(参见下面@ADmad 的答案),“官方”的方式是使用 PRG,个人而言,这并不“感觉”正确,IMO 的“搜索”不应该使用“POST” '?您当然可以创建一个自定义的 formHelper,如果表单类型为“get”,它将自动从 request->query 获取数据。如果实施得当,这对我来说听起来不是很“意大利面条”。
    • 我同意,搜索感觉应该使用GET,我认为我不应该使用插件或组件来做一些看起来应该开箱即用的事情。使用 CodeIgniter 就简单多了。
    • 按照认为最适合您的目的的方式实施。尝试以“体面”的方式实施它以防止意大利面。我很想听听其他人的意见,尽管我的“错误”在于认为“搜索”表单应该简单地使用“获取”:)
    【解决方案2】:

    尝试将此添加到您的控制器:

    $this->request->data = $this->params['url'];
    

    【讨论】:

    • 我尝试在控制器的几个地方添加它,但这似乎不起作用。
    【解决方案3】:

    我的解决方案:

    $this->params->data = array('Tablefilter' => $this->params->query);
    

    其中“Tablefilter”取决于表单定义(通常是模型名称)

    $this->Form->create('Tablefilter', array('type' => 'get'))
    

    【讨论】:

      【解决方案4】:

      使用PRG。结帐this插件。

      【讨论】:

      • PRG 不是只适用于“发布”数据的情况吗?我一直想知道为什么 CakePHP 中的“获取”表单不会自动与相应的查询数据一起传播。在这种情况下,搜索插件可能是一个很好的建议。
      • 是的,我建议张贴表格。基于 GET 数据的预填表单并不常见。但是,如果需要,您可以轻松地制作一个组件来这样做。使用 GET 提交表单时还特别令人烦恼的是,您的字段名称通常是表单 data[Foo][bar]。在 URL 中包含这些内容会很丑陋。
      • +1 在这些字段的“丑陋”格式上(我通常会覆盖搜索名称)。仍然不相信搜索表单的“POST”,因为我认为“POST”(和“PUT”)应该保留用于修改数据,但这可能是一个见仁见智的问题:)
      【解决方案5】:

      我最终做的是遍历 $this->request->query 数组并将这些值传递给匹配的 $this->request->data

      所以我添加了这个:

      foreach($this->request->query as $k => $v){
          $this->request->data['Listing'][$k] = $this->request->query[$k];
      }
      

      最终给了我这个:

      class ListingsController extends AppController {
          public function results() {
      
              foreach($this->request->query as $k => $v){
                  $this->request->data['Listing'][$k] = $this->request->query[$k];
              }
      
              $conditions = array(
                  'Listing.Beds >=' => $this->request->query['beds'], 
                  'Listing.ListingStatus >=' => $this->request->query['status'], 
              );
      
              $this->paginate = array(
                  'conditions' => $conditions,
              );    
      
              $this->set('listings', $this->paginate());
          }
      }
      

      虽然我不知道这是否是最佳或建议的方法,但我不会接受这个作为答案。但它现在对我有用。

      【讨论】:

        【解决方案6】:

        这对我有用:

        $this->request->data['Cluster'] = $this->params->query ; //控制器端

        表格定义:

        $this->Form->create('Cluster',array('type'=>'get'));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多