在 drupal 中获取更改后的表单值

发布于 2024-08-30 18:46:04 字数 96 浏览 4 评论 0原文

我以 drupal 名称创建了一个模块作为 newmodule。我使用 form alter 来更改用户注册表单以添加一个字段位置。当我提交表单时,我如何获取我创建的新字段的值。

I created one module in drupal name as newmodule.I use form alter to alter user registration form for adding one field location.When i submit the form, how i can get the value of the new field which i created .

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

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

发布评论

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

评论(3

不再见 2024-09-06 18:46:04

为了详细说明前面的答案,在您的 hook_form_alter 函数中的某处,您想要告诉表单运行您自己的 #submit 处理程序,例如:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['new_field'] = array( ... ); // You already did this, right?
  $form['#submit'][] = 'mymodule_submit_handler'; // Add this
}

注意:您应该在此处附加 #submit,而不是替换它。然后在您的提交处理程序中,您可以轻松获取该值,例如:

function mymodule_submit_handler($form, &$form_state) {
  $value = $form_state['values']['new_field'];
}

To elaborate on the previous answers, somewhere in your hook_form_alter function, you want to tell the form to run your own #submit handler, e.g.:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['new_field'] = array( ... ); // You already did this, right?
  $form['#submit'][] = 'mymodule_submit_handler'; // Add this
}

Note: you should be appending on #submit here, not replacing it. Then in your submit handler, you can get the value easily, e.g.:

function mymodule_submit_handler($form, &$form_state) {
  $value = $form_state['values']['new_field'];
}
瀟灑尐姊 2024-09-06 18:46:04

表单值

$form_state['values']['field_name']

默认存储在中。如果将 #tree 设置为 TRUE,此行为将会改变,并且值将位于嵌套数组中,而不是平面数组中。

将调用两种类型的函数,您可以在其中访问 $form_state 变量。

  1. 验证函数用于验证表单数据,检查用户输入的数据是否可接受,例如有效的电子邮件地址等。要添加验证函数,请将其添加到表单更改实现中:

    $form['#validate'][] = 'name_of_your_validate_handler';
    
  2. 提交函数用于对包含有效数据的表单进行操作。通常,您将数据插入数据库,在此处设置重定向等,以添加提交功能,将其添加到您的表单更改实现中:

    $form['#submit'][] = 'name_of_your_submit_handler';
    

验证和提交函数都采用相同的参数:

function validate_or_submit_func(&$form, &$form_state) {
  // $form is the form array created by drupal_get_form
  // $form_state contains valuable info like, the submitted values and other options.
}

The form value is stored in

$form_state['values']['field_name']

By default. If you set #tree to TRUE this behavior will change, and the values will be in a nested array instead of a flat one.

Two type of functions will be called, where you have access to the $form_state variable.

  1. Validation functions is used validate the form data, to check if the user inputted data is acceptable, like a valid email address etc. To add a validation function add this in your form alter implementation:

    $form['#validate'][] = 'name_of_your_validate_handler';
    
  2. Submit functions is used to act upon a form with valid data. Typically you insert data into the database, set redirects and such here, to add a submit function add this in your form alter implementation:

    $form['#submit'][] = 'name_of_your_submit_handler';
    

Both validation and submit functions take the same args:

function validate_or_submit_func(&$form, &$form_state) {
  // $form is the form array created by drupal_get_form
  // $form_state contains valuable info like, the submitted values and other options.
}
白首有我共你 2024-09-06 18:46:04

新模块还需要调用 _submit 挂钩,以便您可以转储 $form 和 $form_state 值,以便可以看到它们。

New module also needs a _submit hook invoked so you can dump out the $form and $form_state values so you can see them.

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