验证中的重定向表单失败

发布于 2024-09-06 22:14:01 字数 431 浏览 12 评论 0原文

我遇到的情况是,如果我的表单未验证,我需要它从 url 中删除除第一个参数之外的所有参数。这样做的原因是因为我的表单结果显示在内容区域的表单下方。如果表单未通过验证,我需要删除以前的所有结果。

我在 form_validate 函数中尝试了以下操作。

// Check if there were any errors.
$errors = form_set_error();
if (!empty($errors)) {
  drupal_goto(arg(0));
}

目标页面显然是相同的,只是删除了除第一个参数之外的所有参数。问题是 Drupal 忘记了之前的表单状态以及 form_validate 函数引发的错误。

我还尝试更改 form_validate 函数中的 #redirect 值,但无济于事。

有什么建议吗?

I have a situation that if my form doesn't validate I need it to remove all but the first argument from the url. The reason for this is because the results of my form are displayed below the form in the content area. If the form doesn't validate I need to remove any previous results.

I am have tried the following in my form_validate function.

// Check if there were any errors.
$errors = form_set_error();
if (!empty($errors)) {
  drupal_goto(arg(0));
}

The destination page obviously is the same except that all arguments, except the first, are stripped. The problem is that Drupal forgets about the previous form state and the errors that were raised by the form_validate function.

I have also tried to change the #redirect value in my form_validate function but to no avail.

Any suggestions?

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

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

发布评论

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

评论(2

山色无中 2024-09-13 22:14:01

你已经把自己置于一个糟糕的境地了。 AFAIK,您无法在不丢失 $form_state 的情况下重定向。除非提交表单,否则重定向将不起作用,因此只要表单未验证,您就不会走得太远。

我不知道你如何设置你的代码,但在我看来,你会更好地运气在 $form_state 中设置一个变量,并使用它来确定是否应该隐藏/显示结果。

另一种选择是将 $form_state 保存在全局 $_SESSION['batch_form_state'] 中,以便在启动表单时使用它。我以前没有尝试过,它有点黑客,但它应该可以工作。

You have put yourself in a bad situation. AFAIK, you can't redirect without loosing the $form_state. Redirecting wont work unless the form is submitted, so as long as the form doesn't validate you wont get far.

I don't know how you setup your code, but I seems to me that you will have better luck settings a variable in the $form_state and using that to determine if you should hide/display the results.

An alternative option would be to save the $form_state in the global $_SESSION['batch_form_state'], to have it used when the form is initiated. I haven't tried it before and it's a bit hackish, but it should work.

对你再特殊 2024-09-13 22:14:01

如果表单数据不大,您可以将其存储在 $_SESSION 中。然后检查重定向后是否存在。

我不熟悉 Drupal,但您必须能够访问过滤后的表单值。因此,您无需使用 $_POST 字段进行验证,而是使用存储的 $_SESSION 值进行验证。

简化示例

session_start();

// ... Previous Validation

// Check if there were any errors.
$errors = form_set_error();
if (!empty($errors)) {
    $_SESSION['form_state'] = $form_state;
    drupal_goto(arg(0));
}

重定向后的

session_start();

$form_state = $_SESSION['form_state']);
unset($_SESSION['form_state']);

// The rest of the script....

You could store the form data, if it's not big, in $_SESSION. Then check if it exists after the redirect.

I'm not familiar with Drupal, but you must be able to access the filtered form values. So instead of validating with the $_POST fields you validate with the stored $_SESSION values.

simplified example

session_start();

// ... Previous Validation

// Check if there were any errors.
$errors = form_set_error();
if (!empty($errors)) {
    $_SESSION['form_state'] = $form_state;
    drupal_goto(arg(0));
}

after redirecting

session_start();

$form_state = $_SESSION['form_state']);
unset($_SESSION['form_state']);

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