Drupal 6:仅将值的第一个字符插入MySQL

发布于 2024-08-17 11:52:09 字数 2190 浏览 5 评论 0原文

我正在 CCK 类型上使用 hook_form_alter (对于你们 drupal-ers 来说)。我的节点表单中有一个通常是选择列表的字段。但是,在本例中,我想隐藏选择列表,并使用 SQL 查询在表单中填充其值。

一切都很顺利。我可以看到我想要的值显示在 HTML 源代码中,因此我知道我的查询正在正确执行。但是,当我提交表单时,它仅插入值的第一个字符。我的一些测试的值为 566、784、1004 - 列值分别为 5、7、1。

起初我认为它必须是数据库列属性,但是当我删除使字段隐藏的 form_alter 并手动选择值时,插入了正确的值?!?

   <?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
       if (arg(0) == 'user' && is_numeric(arg(1))) {
        $account = arg(1);
        $club = 2589;
        $form['field_sr_account'] = array( '#type' => 'hidden',
        '#value' => $club
        );

           }
}


?>

谁能明白为什么只插入第一个字符?

注意:我尝试使用 #value & 删除并重新创建该列。 #default_value,它仍然只提交整数的第一个字符。此外,我通过删除提交处理程序作为可能的原因,但这仍然导致仅提交一个字符

更多更新 - 仍在搜索! 好的,有一些好问题。请允许我回答他们:

  1. 数据库列类型是整数(4)
  2. 挂钩生成的 HTML 是:

    input type="hidden" name="field_sr_account" id="edit-field-sr-account" value="2589"

最新更新:我认为问题已缩小到数组的结构。当我在处理表单更改后对此字段执行 var_dump 时,这就是我得到的结果。

[43] => Array
        (
            [#type] => hidden
            [#default_value] => 2589
            [#post] => Array
                (
                )

            [#programmed] =>
            [#tree] =>
            [#parents] => Array
                (
                    [0] => field_sr_account
                )

            [#array_parents] => Array
                (
                    [0] => field_sr_account
                )

            [#weight] => 0.016
            [#processed] => 1
            [#description] =>
            [#attributes] => Array
                (
                )

            [#required] =>
            [#input] => 1
            [#process] => Array
                (
                    [0] => form_expand_ahah
                )

            [#name] => field_sr_account
            [#id] => edit-field-sr-account
            [#value] => 2589
            [#defaults_loaded] => 1
            [#sorted] => 1
        )

我可以将表单值设置为的字段的结构是什么。这一定是像 abhaga 所建议的那样。

I am working with a hook_form_alter on a CCK type (for you drupal-ers). I have a field that is normally a select list in my node form. However, in this instance, I want to hide the select list, and populate its value in the form with an SQL query.

Everything was going nicely. I could see that my desired value was showing up in the HTML source, so I knew my query was executing properly. However, when I submit the form, it only inserts the first character of the value. A few of my tests were values of 566, 784, 1004 - the column values were 5,7,1, respectively.

At first I thought it had to be the DB column attributes, but when I removed my form_alter that makes the field hidden and select the value manually, the correct value is inserted?!?

   <?php
function addSR_form_service_request_node_form_alter(&$form, $form_state) {
       if (arg(0) == 'user' && is_numeric(arg(1))) {
        $account = arg(1);
        $club = 2589;
        $form['field_sr_account'] = array( '#type' => 'hidden',
        '#value' => $club
        );

           }
}


?>

Can anyone see why only the first character would be inserted??

Note: I have tried deleting and recreating the column, using #value & #default_value, and it is still submitting only the first character of the integer. Also, I eliminated the submit handler as a possible cause by removing it, which still resulted in only one character being submitted

More Updates - Still Searching!
Okay, some good questions. Allow me to answer them:

  1. The DB column type is integer(4)
  2. The HTML the hook produces is :

    input type="hidden" name="field_sr_account" id="edit-field-sr-account" value="2589"

Latest Update: I think the issue has been narrowed to the structure of the array. When I do var_dump on this field after the form alter has been processed, this is what I get..

[43] => Array
        (
            [#type] => hidden
            [#default_value] => 2589
            [#post] => Array
                (
                )

            [#programmed] =>
            [#tree] =>
            [#parents] => Array
                (
                    [0] => field_sr_account
                )

            [#array_parents] => Array
                (
                    [0] => field_sr_account
                )

            [#weight] => 0.016
            [#processed] => 1
            [#description] =>
            [#attributes] => Array
                (
                )

            [#required] =>
            [#input] => 1
            [#process] => Array
                (
                    [0] => form_expand_ahah
                )

            [#name] => field_sr_account
            [#id] => edit-field-sr-account
            [#value] => 2589
            [#defaults_loaded] => 1
            [#sorted] => 1
        )

What is the structure of the field that I can set the form value to. It's gotta be something like what abhaga is suggesting..

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

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

发布评论

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

评论(4

那小子欠揍 2024-08-24 11:52:09

由于您尝试更改的字段最初使用选择小部件,因此 CCK 将查找 $form_state['values']['field_sr_account'][0]['value']。通过将字段设置为 #hidden 类型并设置 #value,您将在 $form_state['values']['field_sr_account'] 中获取其值。 CCK 将尝试访问该元素的第一个元素并以该值的第一个字符结束。

更新:实现您需要的最简单的方法是做某事:

function addSR_form_service_request_node_form_alter(&$form, $form_state) {
   if (arg(0) == 'user' && is_numeric(arg(1))) {
    $account = arg(1);
    $club = 2589;
    // Use this property to store the value to restore back
    $form['#field_sr_account'] = $club;
    $form['field_sr_account'] = array( '#type' => 'hidden','#value' => $club);
   }
}

/*in your submit handler, restore the value in the proper format*/
$form_state['values']['field_sr_account'] = array('0' => array('value' => $form['#field_sr_account']));

旧答案

实现你的目标的一种方法
试图做的是复制整个
$form['field_sr_account'] 进入
$form['#field_sr_account'] 然后
通过 SQL 提供值
以正确的格式查询
提交处理程序本身。

Since the field you are trying to change was originally using a select widget, CCK will be looking for $form_state['values']['field_sr_account'][0]['value']. By setting the field to a #hidden type and setting #value, you will get its value in $form_state['values']['field_sr_account']. CCK will try to access the first element of that and end up with the first character of the value.

Updated: The easiest way to achieve what you need would be to do something:

function addSR_form_service_request_node_form_alter(&$form, $form_state) {
   if (arg(0) == 'user' && is_numeric(arg(1))) {
    $account = arg(1);
    $club = 2589;
    // Use this property to store the value to restore back
    $form['#field_sr_account'] = $club;
    $form['field_sr_account'] = array( '#type' => 'hidden','#value' => $club);
   }
}

/*in your submit handler, restore the value in the proper format*/
$form_state['values']['field_sr_account'] = array('0' => array('value' => $form['#field_sr_account']));

Old Answer

One way of accomplishing what you are
trying to do is to copy the whole
$form['field_sr_account'] into
$form['#field_sr_account'] and then
provide the value through the SQL
query in the right format in the
submit handler itself.

扶醉桌前 2024-08-24 11:52:09

好的,看看 http://api.drupal .org/api/drupal/developer--topics--forms_api_reference.html#hiddenhttp://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#value

还建议您使用值而不是隐藏。您可以在 http://api 上找到此信息。 drupal.org/api/drupal/developer--topics--forms_api.html/6

另外,隐藏类型不允许拥有您分配给它的属性,因此这可能会导致问题。您可能在使用表单 API 时遇到的任何使用问题都应该在这些资源中得到解答,因为我仍然不太清楚您要实现的目标......特别是使用提交按钮。

旧答案:

好的,如果我理解正确的话
$club 设置不正确。如果
您查询的第一个结果是
你要找的号码就是这个
应该可以。

尝试打电话

;

查看返回的所有内容
来自查询。

我有点不清楚什么是
设置不正确。如果是
关联数组中的 #value
那么罪魁祸首一定是查询。
如果 #value 设置正确并且
无论你以后用它做什么都可能
是罪魁祸首(此处未显示)。如果
它是您的 $form_state I 中的值
没有看到您在此处使用 $club
完全没有。

此外,在您的addSR_submit_function
您似乎没有使用 $form
变量,或使用 $club 进行任何操作
除了设置消息
显示在您所在页面的顶部
当它被调用时。

我可能需要一些进一步的说明
至于到底出了什么问题。

此外,当您打电话时
drupal_set_message 函数,你是吗?
这样做只是为了调试
目的?

Ok take a look at http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#hidden versus http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html#value

It is also recommended you use value instead of hidden. You can find this info on http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6

Also, type hidden is not allowed to have properties your assigning to it so this may be causing a problem. Any usage problems you may be having with the forms API should be answer in those resources as I"m still a little unclear on what you're trying to accomplish... specifically with the submit button.

Old answer:

Ok if I understand this correctly
$club is not being set correctly. If
the first result from your query is
the number your looking for then this
should work.

Try calling

<?php print_r(db_fetch_array($result)) ?>

to get a look at everything returned
from the query.

I'm a little unclear as to what is
being set incorrectly. If it's
#value inside your associated array
then the culprit must be the query.
If #value is being set correctly and
whatever your doing with it later may
be the culprit (not shown here). If
its the values in your $form_state I
don't see that your using $club here
at all.

Also, in your addSR_submit_function
you don't seem to be using the $form
variable, or using $club for anything
except for setting the message which
appears at the top of the page your on
when it's called.

I may need some further clarification
as to what exactly is going wrong.

Also, when you're calling
drupal_set_message function, are you
just doing this for debugging
purposes?

装纯掩盖桑 2024-08-24 11:52:09

您不应该检查

drupal_set_message($form_state['values']['field_sr_account']);

而不是

drupal_set_message($club);

在 addSR_submit_function 中吗?

好的,只是一个问题:不确定 db_result 为您的查询返回什么类型,可能与类型转换有关?所以这是为了确保 value 是 int 。

'#value' => (int)$club

Shouldn't you check

drupal_set_message($form_state['values']['field_sr_account']);

instead of

drupal_set_message($club);

in addSR_submit_function ?

OK, just a quess: not sure what type db_result returns for your query, may be it has something to do with types conversions? So this is to make sure value is int.

'#value' => (int)$club
魂ガ小子 2024-08-24 11:52:09

cinqoTimo,出于好奇,这是什么样的 CCK 领域?它是整数、小数、浮点数吗?该字段上是否有任何默认情况下通常不打开的特殊参数?数据库中的列类型是什么?

你能发布表单的 html 输出吗?这可能会为可能发生的情况提供线索。

您是否使用任何 JavaScript 来编辑此字段的任何值?

您是否尝试过从 addSR_form_service_request_node_submit 挂钩输出值结果?有什么区别。

抱歉提出所有问题。只是大声思考,因为看起来您已经涵盖了大部分基础。

cinqoTimo, Out of curiosity what kind of CCK field is this? Is it a Integer, Decimal, Float? and do you have any special parameters on that field not normally on by default? What is the column type in the db?

Can you post the html output of the form. That might give a clue as to what might be going on.

Are you using any javascript to edit any values for this field?

Have you tried outputting the value results from addSR_form_service_request_node_submit hook? Any difference there.

Sorry for all the questions. Just thinking out loud as it seems you have covered most of your bases.

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