在 Drupal 7 中,如何在验证值之前更改提交表单的内容?
我想做一些与以下内容大致类似(但不完全相同)的事情:我想创建一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我误解了,请道歉,但您应该能够执行以下操作:
然后您可以在任一中使用
$form['#validate'][] = 'my_validation_handler'
附加该验证函数您的表单构建或 form_alter 函数。希望有帮助
Apologies if I've misunderstood but you should just be able to do something like this:
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
您应该使用
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 :)