在 Drupal 7 中,如何在验证值之前更改提交表单的内容?

发布于 2024-12-14 23:41:17 字数 382 浏览 2 评论 0原文

我想做一些与以下内容大致类似(但不完全相同)的事情:我想创建一个 Person 内容类型,它有一个 SSN 字段。我想将 SSN 字段存储为整数,但允许用户输入数字 123-45-6789。这意味着在验证触发之前,声明“123-45-6789”是无效输入,我想删除破折号并将其视为整数。

我尝试使用 #value_callback 函数以及非默认验证函数。那么问题是,虽然我可以强制验证该值,但未更改的值是传递给数据库进行插入的值,这会失败。例如,这意味着虽然我可以强制“123-45-6789”被 Drupal 识别为“123456789”,但数据库仍然传递“123-45-6789”,这当然会失败。

一个明显的解决方案是在将值提交给网络服务器之前通过客户端 JavaScript 更改此值。我强烈希望避免这条路线。

I would like to do something roughly analogous (but not exactly identical) to the following: I want to create a Person content type, which has an SSN field. I would like to store the SSN field as an integer, but allow the user to input the number as 123-45-6789. This means that before validation triggers, stating that "123-45-6789" is invalid input, I would like to remove the dashes and treat this as an integer.

I've tried to use both a #value_callback function, as well as a non-default validation function. The problem then is that although I can force the value to be validated, the unchanged value is what is passed to the db for insertion, which fails. In example, this means that although I can force "123-45-6789" to be recognized by Drupal as "123456789", the database is still being passed "123-45-6789", which of course fails.

The one obvious solution would be altering this via client side javascript, before the value is even submitted to the webserver. I would strongly prefer to avoid this route.

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

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

发布评论

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

评论(2

执笏见 2024-12-21 23:41:17

如果我误解了,请道歉,但您应该能够执行以下操作:

function my_validation_handler(&$form, &$form_state) {
  if (passes_ssn_validation($form_state['values']['SSN'])) {
    // Changing the value in $form_state here will carry on over to the submission function
    $form_state['values']['SSN'] = convert_to_db_format($form_state['values']['SSN']);
  }
  else {
    form_set_error('SSN', 'The SSN was invalid');
  }
}

然后您可以在任一中使用 $form['#validate'][] = 'my_validation_handler' 附加该验证函数您的表单构建或 form_alter 函数。

希望有帮助

Apologies if I've misunderstood but you should just be able to do something like this:

function my_validation_handler(&$form, &$form_state) {
  if (passes_ssn_validation($form_state['values']['SSN'])) {
    // Changing the value in $form_state here will carry on over to the submission function
    $form_state['values']['SSN'] = convert_to_db_format($form_state['values']['SSN']);
  }
  else {
    form_set_error('SSN', 'The SSN was invalid');
  }
}

Then you'd attach that validation function using $form['#validate'][] = 'my_validation_handler' in either your form build or form_alter function.

Hope that helps

幸福%小乖 2024-12-21 23:41:17

您应该使用hook_node_presave()。它允许您在将不同字段的值插入数据库之前更改它们。这是官方文档:

http: //api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_node_presave/7

希望这可以帮助:)

you should use hook_node_presave(). It allows you to change the values of different fields before they are inserted to the database. Here's the official documentation:

http://api.drupal.org/api/drupal/modules--node--node.api.php/function/hook_node_presave/7

Hope this can help :)

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