如何从自定义模块欺骗 Drupal 6 中的表单值而不抛出“非法选择”错误

发布于 2024-10-13 04:28:58 字数 470 浏览 5 评论 0原文

我有一个网站,它使用带有公开过滤器的视图和 Finder 模块来搜索工作。标准之一是薪资范围,该范围以英镑显示在具有 4 个选项的选择框中。然而,客户要求提供一个允许用户显示不同货币的选项。

我创建了一个模块,允许用户选择不同的货币,进而设置包含货币值的会话变量。当用户查看包含 Finder 搜索表单或视图公开过滤器的页面时,Drupal 会添加一些 JQuery,该 JQuery 会根据会话变量的值更改选择框的可见值。但是,它不会更改选择选项的实际值 - 只会更改用户可以看到的值。

不幸的是,此方法会抛出“已检测到非法选择,请联系站点管理员”错误,显然是因为发布的表单数组与 Drupal 存储的表单数组不匹配。那么问题来了,如何才能阻止这个错误的发生呢?我正在考虑在提交之前由 JQuery 重置表单值,尽管我不确定如何更改用户单击提交按钮和实际提交的表单之间的值。另一种选择可能是 hook_form_alter 来更改表单值,但我不确定这是否有效。

有什么想法吗?

I have a website that uses a View with exposed filters and the Finder module to search for jobs. One of the criteria is salary range, which is displayed in British Pounds in a select box with 4 options. However, the client has asked for an option that allows the user to display a different currency.

I have created a module that allows a user the select a different currency, which in turn sets a session variable containing a currency value. When the user views a page containing either the Finder search form or the Views exposed filter, Drupal adds some JQuery which changes the visible values of the select box based on the value of the session variable. However, it does not change the actual values of the select options - only what the user can see.

Unfortunately this method throws the "illegal choice has been detected please contact the site administrator" error, obviously because the posted form array doesn't match the form array stored by Drupal. So the question is, how can I stop this error from occurring? I was thinking of the form values being reset by JQuery just before submit, although I'm not sure how I would change the values between the user clicking the submit button and the form actually being submitted. Another choice is perhaps hook_form_alter to change the form values, but I'm not exactly sure if this would work either.

Any ideas?

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

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

发布评论

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

评论(1

伪装你 2024-10-20 04:28:58

对于任何感兴趣的人,我创建了一个使用 hook_form_alter 的新模块。该挂钩添加了一个额外的货币按钮,并在提交时设置一个包含货币信息的会话变量。当我的模块读取会话时,它会根据会话值更改实际工资范围字段。

Just for anyone that was interested, I created a new module that uses hook_form_alter. The hook adds an extra button for currency and when submitted, sets a session variable containing the currency information. When my module reads the session, it alters the actual salary band field based on the session value.

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