开发者

Drupal 6 Form API not processing values

I am trying to dynamically generate form elements (based on an array retrieved from DB) using the following code:

    $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'] = arr开发者_如何学Goay
                                    (
                                        '#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',
                                    );
        }
    }

The generated form_state['values] dpm looks like this: (Array, 54 elements)

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 ) 

All the dynamically generated text-fields get a value of 0 regardless of what I type into them.


Because of the way that the Drupal Form API is designed, you cannot have square brackets in the names of the form items. If you change all of your [nid:...]-.. to nid:...-..., it will work fine.

$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',
    );
  }
}

I would also recommend that if you are going to be making a complex form, it is very useful to generate the form values in a hierarchy, so that you don't need a complex structure based on the $form_state['values'] array key like it looks like you are trying to build.

Try something like this, and look at the returned 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',
    );
  }
}

By setting #tree in the 'stats' fieldset, it tells drupal to return the $form_state['values'] in the same hierarchy that the form was it, which makes processing those values much easier. That is the quick explanation anyway, more info can be found here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#tree

Lastly, in the future when you paste in code, please make sure it is indented reasonable. It makes it much harder to read and people are much less likely to answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜