2011-08-16 58 views
0

我使用下面的代碼試圖動態生成表單元素(基於從DB中檢索陣列上):Drupal的6表API不處理值

$form['stats'] = array 
          (
           '#type' => 'fieldset', 
           '#title' => 'Statistics', 
           '#description' => 'Enter the data for the selected stat' 
          ); 

    $arrStats = _stats_getStats($player_id); 
    if ($arrStats!=false) 
    { 

     foreach ($arrStats as $stat) 
     { 
      $arrVal = array(); 
      $player_stats = _stats_getPlayerStats($player_id, $stat->sid); 
      if (!empty($player_stats)) 
       $arrVal = $player_stats; 
      else 
       $arrVal = _stats_getPositionStats ($position->nid, $stat->sid); 

      $form['stats']['[nid:'.$stat->sid.']-1'] = array 
            (
             '#type' => 'textfield', 
             '#title' => $stat->siname." - 1", 
             '#default_value' => $arrVal[0], 
             '#description' => 'Enter 1', 
            ); 
      $form['stats']['[nid:'.$stat->sid.']-2'] = array 
            (
             '#type' => 'textfield', 
             '#title' => $stat->siname." - 2", 
             '#default_value' => $arrVal[1], 
             '#description' => 'Enter 2', 
            ); 
     } 
    } 

生成form_state ['值] DPM看起來像這樣: (陣列,54個元件)

player-id (String, 2 characters) 74 
[nid:90]-1 (String, 0 characters) 
[nid:90]-2 (String, 0 characters) 
[nid:89]-1 (String, 0 characters) 
[nid:89]-2 (String, 0 characters) 
[nid:80]-1 (String, 0 characters) 
[nid:80]-2 (String, 0 characters) 
[nid:79]-1 (String, 0 characters) 
[nid:79]-2 (String, 0 characters) 
[nid:78]-1 (String, 0 characters) 
[nid:78]-2 (String, 0 characters) 
[nid:91]-1 (String, 0 characters) 
[nid:91]-2 (String, 0 characters) 
[nid:92]-1 (String, 0 characters) 
[nid:92]-2 (String, 0 characters) 
[nid:93]-1 (String, 0 characters) 
[nid:93]-2 (String, 0 characters) 
[nid:94]-1 (String, 0 characters) 
[nid:94]-2 (String, 0 characters) 
[nid:95]-1 (String, 0 characters) 
[nid:95]-2 (String, 0 characters) 
[nid:98]-1 (String, 0 characters) 
[nid:98]-2 (String, 0 characters) 
[nid:96]-1 (String, 0 characters) 
[nid:96]-2 (String, 0 characters) 
[nid:97]-1 (String, 0 characters) 
[nid:97]-2 (String, 0 characters) 
[nid:99]-1 (String, 0 characters) 
[nid:99]-2 (String, 0 characters) 
[nid:141]-1 (String, 0 characters) 
[nid:141]-2 (String, 0 characters) 
[nid:143]-1 (String, 0 characters) 
[nid:143]-2 (String, 0 characters) 
[nid:146]-1 (String, 0 characters) 
[nid:146]-2 (String, 0 characters) 
[nid:147]-1 (String, 0 characters) 
[nid:147]-2 (String, 0 characters) 
[nid:149]-1 (String, 0 characters) 
[nid:149]-2 (String, 0 characters) 
[nid:150]-1 (String, 0 characters) 
[nid:150]-2 (String, 0 characters) 
[nid:151]-1 (String, 0 characters) 
[nid:151]-2 (String, 0 characters) 
[nid:144]-1 (String, 0 characters) 
[nid:144]-2 (String, 0 characters) 
[nid:145]-1 (String, 0 characters) 
[nid:145]-2 (String, 0 characters) 
[nid:148]-1 (String, 0 characters) 
[nid:148]-2 (String, 0 characters) 

所有動態生成的文本字段得到的值0,無論什麼I型放進去。

回答

0

由於Drupal Form API的設計方式,您不能在表單項的名稱中包含方括號。如果您將所有[nid:...] - ..更改爲nid:...-...,則它將正常工作。

$form['stats'] = array(
    '#type'  => 'fieldset', 
    '#title'  => 'Statistics', 
    '#description' => 'Enter the data for the selected stat' 
); 

$arrStats = _stats_getStats($player_id); 
if ($arrStats != false) { 

    foreach ($arrStats as $stat) { 
    $player_stats = _stats_getPlayerStats($player_id, $stat->sid); 

    $arrVal = empty($player_stats) ? _stats_getPositionStats($position->nid, $stat->sid) : $player_stats; 

    $form['stats']['nid:' . $stat->sid . '-1'] = array(
     '#type'   => 'textfield', 
     '#title'   => $stat->siname." - 1", 
     '#default_value' => $arrVal[0], 
     '#description' => 'Enter 1', 
    ); 
    $form['stats']['nid:'.$stat->sid.'-2'] = array(
     '#type'   => 'textfield', 
     '#title'   => $stat->siname." - 2", 
     '#default_value' => $arrVal[1], 
     '#description' => 'Enter 2', 
    ); 
    } 
} 

我也建議,如果你將要作出一個複雜的形式,這是非常有用的生成層次結構形式的值,這樣你就不需要基於$複雜的結構form_state ['values']數組鍵看起來像你正在試圖建立。

嘗試這樣的事情,看看返回的值。

$form['stats'] = array(
    '#type'   => 'fieldset', 
    '#title'  => 'Statistics', 
    '#description' => 'Enter the data for the selected stat', 
    '#tree'   => TRUE,  // Set #tree here 
); 

$arrStats = _stats_getStats($player_id); 
if ($arrStats != false) { 

    foreach ($arrStats as $stat) { 
    $player_stats = _stats_getPlayerStats($player_id, $stat->sid); 

    $arrVal = empty($player_stats) ? _stats_getPositionStats($position->nid, $stat->sid) : $player_stats; 

    $form['stats'][$stat->sid][1] = array( // Use standard nested arrays here 
     '#type'   => 'textfield', 
     '#title'   => $stat->siname." - 1", 
     '#default_value' => $arrVal[0], 
     '#description' => 'Enter 1', 
    ); 
    $form['stats'][$stat->sid][2] = array( // and here 
     '#type'   => 'textfield', 
     '#title'   => $stat->siname." - 2", 
     '#default_value' => $arrVal[1], 
     '#description' => 'Enter 2', 
    ); 
    } 
} 

通過在「統計」字段集設置#tree,它告訴Drupal能夠在同一層級的形式就是這樣,這使得處理這些值更容易返回$ form_state [「值」]。無論如何,這是快速的解釋,更多的信息可以在這裏找到:http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#tree

最後,在將來你粘貼代碼時,請確保它縮進合理。這使得閱讀變得更加困難,並且人們不太可能回答。

+0

爲我節省了一天的調試時間。非常感謝你。 – user897452