在 Drupal 中将表单元素作为数组

发布于 2024-10-14 05:21:00 字数 538 浏览 2 评论 0原文

使用 Drupal 6.20。

我们可以像这样设置一些表单元素:-

<input type="select" name="somename[]"><option>ohai</option></select>

然后在 PHP 中循环它们,

foreach ($somename as $name) { ... }

我试图在 Drupal 中做同样的事情。我有一个样式相同的 select-elements 列表。元素的数量将来可能会发生变化,因此表单处理必须是动态的。

如果我使用上述方法,每个元素都会覆盖前一个元素,因此最终只有一个元素打印到屏幕上。我无法编写 name="somename[$someid]" 因为这不会将 $somename 解释为数组。
Drupal 支持这个还是我在做是吗?

另外,还有其他替代方法可以实现相同的目的吗?

Using Drupal 6.20.

We can setup some form elements like this:-

<input type="select" name="somename[]"><option>ohai</option></select>

and then cycle through them in PHP with

foreach ($somename as $name) { ... }

I am trying to do the same in Drupal. I have a list of select-elements that are identical in style. The number of elements may change in the future so the form processing has to be dynamic.

If I use the above approach, each element will overwrite the preceding one, so that ultimately only one element is printed to the screen. I can't write name="somename[$someid]" as that will not interpret $somename as an array.
Does Drupal support this or am I doing it worng?

Also, Is there any other alternative to achieve the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提笔落墨 2024-10-21 05:21:00

这是一个实现您想要做的事情的示例。


function test_form( &$form_state )
{
  $form = array();
  $delta = 0;
  $form["test_field"]["#tree"] = TRUE;
  $form["test_field"][$delta++] = array(
        "#type" => "textfield",
        "#title" => "Title",
    );
  $form["test_field"][$delta++] = array(
        "#type" => "textfield",
        "#title" => "Title",
    );
  $form["test_field"][$delta++] = array(
        "#type" => "textfield",
        "#title" => "Title",
    );
  $form["submit"] = array(
        "#type" => "submit",
        "#value" => "Submit",
    );
  return $form;
}

在您的提交中验证函数,您将在字段名称下获得一个值数组。

请记住,在元素上启用 #tree 是此方法的关键。另外,Drupal 的表单 API 是我使用过的最好的表单框架之一。

希望这有帮助。

Here is an example to achieve what you are trying to do.


function test_form( &$form_state )
{
  $form = array();
  $delta = 0;
  $form["test_field"]["#tree"] = TRUE;
  $form["test_field"][$delta++] = array(
        "#type" => "textfield",
        "#title" => "Title",
    );
  $form["test_field"][$delta++] = array(
        "#type" => "textfield",
        "#title" => "Title",
    );
  $form["test_field"][$delta++] = array(
        "#type" => "textfield",
        "#title" => "Title",
    );
  $form["submit"] = array(
        "#type" => "submit",
        "#value" => "Submit",
    );
  return $form;
}

In your submit & validate function, you will get an array of values under your field's name.

Remember, enabling #tree on your element is the key to this approach. Also Drupal's form API is one of the best forms framework I have worked with.

Hope this helps.

梦旅人picnic 2024-10-21 05:21:00

我知道这个问题已得到解答,但我认为有一种更简单的方法,该方法不太冗长,您只需要更改字段的数量(或在获取表单时将其作为参数传递)。

function test_form() 
{
    $form['#tree'] = TRUE; // This is to prevent flattening the form value
    $no_of_fields = 5;     // The number of fields you wish to have in the form

    // Start adding the fields to the form
    for ($i=1; $i<=$no_of_fields; $i++)
    {
        $form['somename'][$i] = array(
            '#title' => t('Test field no. '.$i),
            '#type'  => 'textfield',
        );      
    }
    // Add the submit button
    $form["submit"] = array(
        "#type"  => "submit",
        "#value" => "Submit",
    );  
}

提交时,您的 $form_state['values'] 将包含(除其他外)您的表单元素值作为数组:

  'somename' => 
    array
      1 => string '' (length=0)
      2 => string '' (length=0)
      3 => string '' (length=0)
      4 => string '' (length=0)
      5 => string '' (length=0)

I know this question was answered but I think there's an easier way which is less verbose and you only need to change the number of fields (or pass it as an argument when getting the form).

function test_form() 
{
    $form['#tree'] = TRUE; // This is to prevent flattening the form value
    $no_of_fields = 5;     // The number of fields you wish to have in the form

    // Start adding the fields to the form
    for ($i=1; $i<=$no_of_fields; $i++)
    {
        $form['somename'][$i] = array(
            '#title' => t('Test field no. '.$i),
            '#type'  => 'textfield',
        );      
    }
    // Add the submit button
    $form["submit"] = array(
        "#type"  => "submit",
        "#value" => "Submit",
    );  
}

When submitting your $form_state['values'] will contain (among other things) your form elements value as an array:

  'somename' => 
    array
      1 => string '' (length=0)
      2 => string '' (length=0)
      3 => string '' (length=0)
      4 => string '' (length=0)
      5 => string '' (length=0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文