以编程方式跳过默认 CCK 表单验证

发布于 2024-12-16 10:26:46 字数 422 浏览 0 评论 0原文

背景:在 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 技术交流群。

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

发布评论

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

评论(2

合约呢 2024-12-23 10:26:46

设置 $form['#validate'] = array(); 应该在一定程度上起作用(任何显式验证处理程序将不再运行),但还有 #element_validate 键可以添加到大多数元素,#required 标志将启动默认表单验证。

我认为消除这些约束的最简单方法是递归地运行表单并取消设置非法值。像这样的事情:

function mymodule_unrequire_element(&$element) {
  if (isset($element['#required'])) {
    unset($element['#required']);
  }
  if (isset($element['#element_validate'])) {
    unset($element['#element_validate']);
  }

  foreach (element_children($element) as $child_element) {
    mymodule_unrequire_element($element[$child_element]);
  }
}

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'the_form_id') {
    mymodule_unrequire_element($form);
  }
}

这完全未经测试,但我认为它会成功:)

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:

function mymodule_unrequire_element(&$element) {
  if (isset($element['#required'])) {
    unset($element['#required']);
  }
  if (isset($element['#element_validate'])) {
    unset($element['#element_validate']);
  }

  foreach (element_children($element) as $child_element) {
    mymodule_unrequire_element($element[$child_element]);
  }
}

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'the_form_id') {
    mymodule_unrequire_element($form);
  }
}

That's completely untested but I think it'll do the trick :)

与往事干杯 2024-12-23 10:26:46

模块 http://drupal.org/project/skip_validation 提供跳过验证功能。看看它可能会有所帮助。

The module http://drupal.org/project/skip_validation offer skip validation functionality. Looking at it may be helpful.

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