Drupal Form API 和 $form_state['storage'] 在页面刷新时被破坏

发布于 2024-08-23 01:26:35 字数 1927 浏览 6 评论 0原文

我有一个显示两个提交按钮的表单。第一个提交按钮将 $form_state['storage'] 设置为一个值。然后第二个提交按钮读取此 $form_state['storage'] 值。如果设置了该值,则会显示成功消息。如果未设置该值,则会显示失败消息。

这是重现我的问题的代码:

function mymodule_test_admin() {
    return drupal_get_form('mymodule_test_form');
}

function mymodule_test_form(&$form_state) {
    $form['mymodule_test_form1'] = array(
        '#type' => 'fieldset',
        '#title' => t('test 1'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE
    );

    $form['mymodule_test_form1']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('button 1'),
        '#submit' => array('mymodule_test_form1_submit')
    );

    $form['mymodule_test_form2'] = array(
        '#type' => 'fieldset',
        '#title' => t('test 2'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE
    );

    $form['mymodule_test_form2']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('button 2'),
        '#submit' => array('mymodule_test_form2_submit')
    );

    return $form;
}

function mymodule_test_form1_submit($form, &$form_state) {
    $form_state['storage']['test_1'] = 'test 1';
    drupal_set_message(t('@message', array('@message' => $form_state['storage']['test_1'])));
}

function mymodule_test_form2_submit($form, &$form_state) {
    if (isset($form_state['storage']['test_1'])) {
        drupal_set_message(t('success'));
    }    else {
            drupal_set_message(t('fail!'));
        }
}

当您单击第一个提交按钮时, $form_state['storage'] 已正确设置。当您单击第二个提交按钮时,将显示消息“成功”。到目前为止,一切都很好。现在进行页面刷新。消息“失败!”显示。

所以一切正常,直到页面刷新。页面刷新本质上只是调用第二个提交函数。理论上, $form_state['storage'] 仍应填充,并且显示的消息应为“成功”。但是,查看 $form_state 转储显示页面刷新后 $form_state['storage'] 为 NULL。我无法判断我的代码逻辑是否错误,或者 $form_state['storage'] 是否在页面刷新时被清除。

有什么想法吗?

感谢您的帮助。

I have a form that displays two submit buttons. The first submit button sets $form_state['storage'] to a value. The second submit button then reads this $form_state['storage'] value. If the value is set, then a success message is displayed. If the value is not set, then a fail message is displayed.

Here is the code that will reproduce my issue:

function mymodule_test_admin() {
    return drupal_get_form('mymodule_test_form');
}

function mymodule_test_form(&$form_state) {
    $form['mymodule_test_form1'] = array(
        '#type' => 'fieldset',
        '#title' => t('test 1'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE
    );

    $form['mymodule_test_form1']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('button 1'),
        '#submit' => array('mymodule_test_form1_submit')
    );

    $form['mymodule_test_form2'] = array(
        '#type' => 'fieldset',
        '#title' => t('test 2'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE
    );

    $form['mymodule_test_form2']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('button 2'),
        '#submit' => array('mymodule_test_form2_submit')
    );

    return $form;
}

function mymodule_test_form1_submit($form, &$form_state) {
    $form_state['storage']['test_1'] = 'test 1';
    drupal_set_message(t('@message', array('@message' => $form_state['storage']['test_1'])));
}

function mymodule_test_form2_submit($form, &$form_state) {
    if (isset($form_state['storage']['test_1'])) {
        drupal_set_message(t('success'));
    }    else {
            drupal_set_message(t('fail!'));
        }
}

When you click the first submit button, $form_state['storage'] is properly set. When you click the second submit button, the message "success" is displayed. So far so good. Now do a page refresh. The message "fail!" is displayed.

So everything works right up until the page refresh. The page refresh essentially is only calling the second submit function. In theory, $form_state['storage'] should still be populated and the message displayed should be "success". However, taking a look at the $form_state dump shows $form_state['storage'] is NULL after the page refresh. I can't tell if my code logic is wrong or if $form_state['storage'] is being cleared on the page refresh.

Any ideas?

Thanks for your help.

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

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

发布评论

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

评论(2

一念一轮回 2024-08-30 01:26:35

您需要在处理 form1_submit 结束时重建表单,这将使之前提交的值保持不变。这是多步骤形式的场景,但与 Drupal 5 中的完成方式有点不同。


function mymoduel_test_form1_submit($form, &$form_state) {
    $form_state['storage']['test_1'] = 'test 1';
    drupal_set_message(t('@message', array('@message' => $form_state['storage']['test_1'])));
    $form_state["rebuild"] = TRUE;
}

希望这会有所帮助,
萨法拉兹

You will need to rebuild form at the end of processing form1_submit, this will keep the previously submitted values intact. This is some what a scenario of multistep forms, but a bit different from the way it was done in Drupal 5.


function mymoduel_test_form1_submit($form, &$form_state) {
    $form_state['storage']['test_1'] = 'test 1';
    drupal_set_message(t('@message', array('@message' => $form_state['storage']['test_1'])));
    $form_state["rebuild"] = TRUE;
}

Hope this helps,
Sarfaraz

小…红帽 2024-08-30 01:26:35

提交后的存储将被清除,使用 $_SESSION['mymodule_test_XXX'] 以多步骤形式存储...

Storage after submit will cleared, use $_SESSION['mymodule_test_XXX'] for storing in multistep forms...

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