2013-04-20 69 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');