如何替换 Drupal Webform 验证器中的字段值?

发布于 2024-11-24 08:19:01 字数 329 浏览 3 评论 0原文

在 Drupal Webform 中,我想在通过验证器时更改提交的值(例如,删除任何非数字字符)。

我按照验证器自述文件添加 hook_webform_validation_validators 并实现 hook_webform_validation_validate 挂钩。但是我无法找到返回参数来更改提交的 Web 表单值。

例如,如果用户输入 $12,340,我希望提交失败并将 Webform 值更新为 12340。当用户第二次提交时,新值12340 将通过验证器并被保存。

In a Drupal Webform, I'd like to alter the submitted value (e.g. to strip any non-numeric character) when going through the validator.

I follow the validator readme to add in hook_webform_validation_validators and implement the hook_webform_validation_validate hook. However I cannot locate a return parameter to alter the submitted webform value.

For example, if a user enters $12,340, I'd like to fail the submission and update the webform value to 12340.When the user submits the second time, the new value 12340 will pass the validator and be saved.

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

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

发布评论

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

评论(1

靑春怀旧 2024-12-01 08:19:01

我认为 Webform Validation 模块不允许您更改提交的值。我已经研究了它如何实现验证,如果您想更改提交的值,您可以在自己的模块中执行类似的操作。

以下代码部分取自 http://fleetthought.com /adding-additional-cck-validation-function-field-using-hookformalter 也来自 Webform Validation 模块代码。

function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') !== FALSE) {
    // Simply add an additional link validate handler.
    $first = array_shift($form['#validate']);
    array_unshift($form['#validate'], $first, 'form_alterations_link_validate');
  }
}

function form_alterations_link_validate($form, &$form_state) {
  // Access submitted values through $form_state['values']['submitted']
}

form_alterations_link_validate 中,您可以使用 Drupal 的 form_set_value() 方法在表单验证期间更改提交的表单值。

I don't think that the Webform Validation module allows you to change the submitted values. I've looked at how it implements the validation and you can do something similar in your own module if you want to change the submitted value.

The following code is taken in part from http://fleetthought.com/adding-additional-cck-validation-function-field-using-hookformalter and also from the Webform Validation module code.

function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'webform_client_form_') !== FALSE) {
    // Simply add an additional link validate handler.
    $first = array_shift($form['#validate']);
    array_unshift($form['#validate'], $first, 'form_alterations_link_validate');
  }
}

function form_alterations_link_validate($form, &$form_state) {
  // Access submitted values through $form_state['values']['submitted']
}

In the form_alterations_link_validate, you can use Drupal's form_set_value() method to change submitted form values during form validation.

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