Drupal 不会让 JQuery 添加到自定义表单中的选择列表
我有一个带有下拉“选择”元素的表单,用户有时需要添加该元素。因此,我添加了一个链接来打开模态框架表单,创建了模态框架表单,并使其 _submit() 函数将新数据添加到保存选择元素选项的表中。然后,它将新的 ID 和名称“返回”给 Javascript 回调(模态框架的方式),JS 回调将新元素添加到下拉列表中,并使用 JQuery 使其成为浏览器中当前选定的元素。这一切都很好。
最后,用户提交原始表单,并收到错误“检测到非法选择。请联系网站管理员”。
表单构建函数从数据库创建选项列表,其中(我检查过)确实包含新选项,所以我猜测 Drupal 正在使用表单的缓存版本,而不是在自动验证之前重新构建它。如果您返回一页,然后前进,新添加的选项就会出现在列表中。
我尝试添加:
global $GLOBALS;
$GLOBALS['conf']['cache'] = FALSE;
到构建有问题的页面的函数,但没有什么区别。
仅供参考:如果重要的话,这是多页表单的一部分。
我还考虑过尝试在“子”表单 _submit() 函数中将选项添加到“父”表单的缓存版本,但不知道如何获取“父”表单的 form_build_id。此外,这似乎太过于拼凑(尽管我可能是错的)。
有想法吗?
I have a form with a drop-down 'select' element that the user will sometimes need to add to. So, I added a link to open a Modal Frame form, created the Modal Frame form, and made its _submit() function add the new data to the table that holds options for the select element. Then it "returns" the new ID and name to the Javascript callback (the way Modal Frames do), and the JS callback adds the new element to the dropdown, and makes it the currently selected element in the browser, with JQuery. This all works great.
Finally, the user submits the original form, and gets the error "An illegal choice has been detected. Please contact the site administrator."
The form building function creates the option list from the database, which (I checked) DOES include the new option, so I'm guessing Drupal is using a cached version of the form rather than re-building it before it does the automatic validation. If you go back one page, then forward, the newly-added choice is there in the list.
I tried adding:
global $GLOBALS;
$GLOBALS['conf']['cache'] = FALSE;
to the function that builds the page with the problem, but it made no difference.
FYI: this is part of a multi-page form, if it matters.
I also thought about trying to add the option to the cached version of the "parent" form in the "child" form _submit() function, but don't know how to get the form_build_id of the "parent" form. Besides, that seems like way too much of a kludge (though I could be wrong about that).
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您提交的表单与表单呈现的时间不同,因此 Drupal 会将其视为恶意输入。如果您想跳过该恶意检查,请在 your_module_form() 函数中添加以下行作为表单的属性。
'#DANGEROUS_SKIP_CHECK' =>是的,
这将解决您的问题,但请确保您添加手动验证以防止恶意输入:)。希望这会对您有所帮助。
You are submitting the form which is different then the time of form render so Drupal consider it as malicious input. If you want to skip that malicious check then add below line as property of your form in your_module_form() function.
'#DANGEROUS_SKIP_CHECK' => TRUE,
This will solve your problem but make sure that you are adding manual validation to protect from malicious inputs :). Hope this will help you.
我认为您最好使用 Drupal 的 AHAH 函数从回调函数请求更新的选择元素。这样,缓存的表单就会在服务器端重建,并且您不需要添加可怕的 DANGEROUS_SKIP_CHECK。这是 Drupal 6 的教程:
http://randyfay.com/ahah
I think you'd be better off using Drupal's AHAH functions to request an updated select element from a callback function. This way the cached form is rebuilt on the server side and you don't need to add the dreaded DANGEROUS_SKIP_CHECK. Here's a tutorial for Drupal 6:
http://randyfay.com/ahah