带有值为 0 的复选框的 drupal 表单提交问题

发布于 2024-08-11 13:20:04 字数 798 浏览 7 评论 0原文

我正在编写一个 drupal 模块,其中涉及一个带有许多复选框的表单。例如,

$form['myform_checkboxes'] = array('#type' => 'checkboxes', ...)

我已将这些复选框的键设置为数字,从 0 开始。例如,

$form['myform_checkboxes']['#options'][0] = '0:00';
$form['myform_checkboxes']['#options'][1] = '1:00';

在实现 myform_checkboxes_submit 函数后,我发现很难解释用户的输入是什么。在互联网上,我发现了几行很好的代码,可以满足我的需要。

$checked = array_intersect(
  array_keys($form_state['values']['myform_checkboxes']),
  array_values($form_state['values']['myform_checkboxes'])
);

这看起来效果很好; $checked 变量是一个仅包含选中的复选框的数组。唯一的问题是值 0(代表第 0 个复选框)总是包含在 $checked 中,无论它是否实际被选中。

还需要注意的是:如果已选中,则零将出现在列表中的第一个位置,如果没有选中,则将出现在最后一个位置。

假设不可能更改复选框的索引,那么解决这种情况的最佳方法是什么? (相关奖励问题:是否有更简单的方法从 drupal 表单变量中获取用户的复选框?)

I am writing a drupal module that involves a form with many checkboxes. E.g.

$form['myform_checkboxes'] = array('#type' => 'checkboxes', ...)

I have made the key for these checkboxes numeric, starting with 0. E.g.

$form['myform_checkboxes']['#options'][0] = '0:00';
$form['myform_checkboxes']['#options'][1] = '1:00';

Upon implementing the myform_checkboxes_submit function, I have discovered that it is difficult to interpret what the user's input was. On the interwebs, I found a few nice lines of code that did what I needed.

$checked = array_intersect(
  array_keys($form_state['values']['myform_checkboxes']),
  array_values($form_state['values']['myform_checkboxes'])
);

This seems to work great; the $checked variable is an array including only the checked checkboxes. The only problem is that the value 0 (representing the 0th checkbox) is always included in $checked, whether it was actually checked or not.

Also useful to note: the zero appears first in the list if it is was checked, but last if it is not.

What would be the best way to resolve this situation, assuming that changing the indexing of the checkboxes were out of the question? (Related bonus question: is there an easier way to get the user's checked boxes from the drupal form variables?)

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

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

发布评论

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

评论(3

私野 2024-08-18 13:20:04

由于未选中复选框的返回值为 0,因此如果您也使用 0 作为其返回值,则无法识别选中状态。因此,您的问题的直接答案是,没有最好的方法,只是因为没有没有方法(除了 Jeremy 建议的 js 解决方法,这将是一个简单问题的相当复杂的解决方案)。

因此,如果您需要结果数组从索引 0 开始,则必须在构建表单选项时暂时删除 0 条目,并在提取结果后将其放回原处。一种简单的方法是使用 -1(或数组其余部分中未使用的任何其他值)作为 0 的占位符,在提取检查的值后再次替换它。

正如 Jeremy 提到的,另一个明显的解决方案是重新索引。看看您的示例,为什么不只使用显示值(0:00、1:00,...)作为键/返回值?那里没有歧义,并且如果需要的话很容易转换为整数。

As the value returned by an unchecked checkbox is 0, there is no way to recognize the checked state if you use 0 as the return value for that also. So the immediate answer to your question is that there is no best way, simply because there is no way (apart from a js workaround as suggested by Jeremy, which would be a pretty complicated solution to a simple problem).

So if you need your result array to start with index 0, you will have to get rid of the 0 entry temporarily when building the form options, putting it back in after extracting the results. An easy way to do this would be to use -1 (or any other value not used in the rest of the array) as a placeholder for 0, replacing it again after extracting the checked values.

Another obvious solution would be reindexing, as mentioned by Jeremy. Looking at your example, why don't you just use your display values (0:00, 1:00, ...) as keys/return values also? No ambiguity there, and easy to convert to integers, if need be.

过期情话 2024-08-18 13:20:04

事实上,我最终所做的就是这样的。

if (in_array(0, $checked) && $checked[0] != 0) {
  unset($checked[count($checked) - 1]);
}

它检查 0 是否在数组中,如果它不是第一项,那么它一定是最后一项(当用户没有选中与 0 对应的框时会发生这种情况)。因此它从数组中删除该项目,因为它没有被检查。不理想或不漂亮,但它对我的情况有意义。

在大多数其他情况下(也许在我的情况下),正如已经指出的那样,重新索引会更好。

编辑:对于任何关心的人,这是我最终为自己创建的辅助功能(包括评论)。

function _mymodule_get_checked_checkboxes(&$form_state, $table) {

  // Discover which boxes were checked.
  $checked = array_intersect(array_keys($form_state['values'][$table]),
                          array_values($form_state['values'][$table]));

  // The key '0' is included in the first position if it was selected,
  //  and in the last if it was not.
  //  this is how checkboxes return their data.
  //  However, we don't want 0 to be in the array
  //  therefore, we remove it if 0 is found to be in the last position
  $num_checked = count($checked);
  if ($checked[0] != 0 && $checked[$num_checked - 1] == 0) {
    unset($checked[count($checked) - 1]);
  }
  // It also happens if nothing is selected.
  // In the case that only 0 is selected, assume otherwise.
  else if ($num_checked == 1 && $checked[0] == 0) {
    unset($checked[0]);
  }

  sort($checked);
  return $checked;
}

Actually, what I ended up doing was this.

if (in_array(0, $checked) && $checked[0] != 0) {
  unset($checked[count($checked) - 1]);
}

It checks if 0 is in the array, and if it's not the first item, then it must be the last (this happens when the user doesn't check the box corresponding to 0). So it removes that item from the array, since it was not checked. Not ideal or pretty, but it made sense for my situation.

In most other situations (and perhaps in mine), as has been pointed out, re-indexing would be better.

EDIT: for anyone who cares, this is the helper function I ended up creating for myself (comments included).

function _mymodule_get_checked_checkboxes(&$form_state, $table) {

  // Discover which boxes were checked.
  $checked = array_intersect(array_keys($form_state['values'][$table]),
                          array_values($form_state['values'][$table]));

  // The key '0' is included in the first position if it was selected,
  //  and in the last if it was not.
  //  this is how checkboxes return their data.
  //  However, we don't want 0 to be in the array
  //  therefore, we remove it if 0 is found to be in the last position
  $num_checked = count($checked);
  if ($checked[0] != 0 && $checked[$num_checked - 1] == 0) {
    unset($checked[count($checked) - 1]);
  }
  // It also happens if nothing is selected.
  // In the case that only 0 is selected, assume otherwise.
  else if ($num_checked == 1 && $checked[0] == 0) {
    unset($checked[0]);
  }

  sort($checked);
  return $checked;
}
指尖微凉心微凉 2024-08-18 13:20:04

您可以使用 JS 提交处理程序来检查复选框的选中状态并将值放入隐藏字段中。

然后,您需要使用 Drupal 表单处理程序来解码另一端的值。

You could use a JS submit handler to check the checked state of the checkboxes and put the values into a hidden field.

You would then need to use a Drupal form handler to decode the values on the other end.

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