以编程方式跳过默认 CCK 表单验证
背景:在 Drupal 6 中,我有一个很长的 CCK 表单,其中包含许多必填字段。我还有一个模块可以授予某些用户特殊权限。一项权限应允许用户跳过必填字段并仍然能够提交表单。我想警告用户,他们跳过了一些(对于非特权用户)在允许他们确认提交之前必需的字段。
问题:如何跳过 CCK 表单的默认验证? (特别是必填字段)
先前的研究:我已经彻底搜索了这个问题的答案。我知道我应该使用 hook_form_alter() ,也可能使用 after_build() 。我已经尝试使用重置验证
$form['#validate'] = array();
,但是验证的行为方式没有变化(例如,必填字段的错误仍然存在,没有发生提交)。
Background: In Drupal 6, I have a very long CCK form with many required fields. I also have a module that grants some users special permissions. One permission should allow the user to skip the required fields and still be able to submit the form. I would like to warn the user that they skipped some fields that (to non-privileged users) are required before allowing them to confirm submission.
Question: How do I skip the default validation for a CCK form? (specifically, required fields)
Previous research: I have already searched thoroughly for an answer to this. I am aware that I should be using hook_form_alter() and probably after_build(), as well. I have already tried to reset the validation using
$form['#validate'] = array();
however there was no change in the way the validation behaved (e.g. errors for required fields remained, no submission ocurred).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置
$form['#validate'] = array();
应该在一定程度上起作用(任何显式验证处理程序将不再运行),但还有#element_validate
键可以添加到大多数元素,#required
标志将启动默认表单验证。我认为消除这些约束的最简单方法是递归地运行表单并取消设置非法值。像这样的事情:
这完全未经测试,但我认为它会成功:)
Setting
$form['#validate'] = array();
should work to a degree (any explicit validation handlers will no longer be run), but there's also the#element_validate
key which can be added to most elements, and the#required
flag which will set off the default form validation.The easiest way I can think to remove those constraints is to recursively run through the form and unset the rogue values. Something like this:
That's completely untested but I think it'll do the trick :)
模块 http://drupal.org/project/skip_validation 提供跳过验证功能。看看它可能会有所帮助。
The module http://drupal.org/project/skip_validation offer skip validation functionality. Looking at it may be helpful.