2013-04-20 72 views
0

我有以下CakePHP选择框。CakePHP - 选择框项目在POSTED数据中没有正确显示

//Users/settings.ctp 

$options = array('NYC', 'LA');  

    echo $this->Form->create('Location'); 
    echo $this->Form->input('Location', array('type' => 'select', 'options' => $options)); 
    echo $this->Form->end(__('Submit')); 

在UsersController我

public function settings($id = null){ 
    if ($this->request->is('post')) { 
     $location = $this->request->data; 
     //$location = $location['Location']['Location'][0]; 
     $this->Session->setFlash(__(print_r($location))); //displays '1' in the alert no matter the selection 
    } 
} 

当我使用的print_r对原始数据就说明如下。

Array ([Location] => Array ([Location] => 0)) 

所以,我有两个问题

  1. 该项目的索引被选中,而不是项目本身
  2. 的setFlash窗口总是显示“1”。我需要做一些字符串操作后,我得到的列表框工作,很高兴看到输出。

更新 - 我走进/Cake/View/Helper/FormHelper.php并做了一些挖

我做了以下行

$attributes = $this->_initInputField($fieldName, array_merge(
     (array)$attributes, array('secure' => self::SECURE_SKIP) 
    )); 

这就造成了一个

的print_r
Array ([value] => 0 [class] => [name] => data[Users][Location] [id] => UsersLocation) 

传递的值为0,我没有看到任何位置

+0

不要在里面setFlash调试需要这么格式化为

$options = array( 'value' => 'Display'); 

的数组,而只是做了'调试($这个 - >请求 - >数据)'来查看发布的数据。 – Oldskool 2013-04-20 23:34:32

+0

这和我上面列出的print_r是一样的。它发送索引,而不是项目。 – user1443519 2013-04-20 23:43:55

回答

0

您正在创建的“位置”模型的形式,但你需要指定选择是哪场:

echo $this->Form->input('Location.location', array('type' => 'select', 'options' => $options)); 

或者,如果它不是真正的“位置”的模式,然后请使用正确的型号替换create()表格中的“位置”,或者使用null,如果它不是型号:

$ this-> Form-> create(null);

+0

没有工作。顺便说一句,这是UsersController的一部分。我没有一个位置,所以如果有的话,它将是用户模型。但是,为什么它会得到索引而不是项目?很显然,如果选定的索引显示正确,就可以看到数据。 – user1443519 2013-04-20 23:58:02

+0

我更新了上面的代码,您能否看到编辑? – user1443519 2013-04-21 00:23:29

0

我想通了,并决定别人可能会觉得它很有用。

,我用

$options = array(
    'NYC' => 'NYC', 
    'LA' => 'LA');