带有附加参数的表单提交处理程序

发布于 2024-09-14 16:00:33 字数 271 浏览 3 评论 0原文

对于某些要求,我需要传递附加信息来表单提交处理程序。在表单 api 中,同时定义自定义提交处理程序,因为

$additional_args = array();
$form['#submit'][] = 'my_submit_handler'

我希望将处理程序提交为

function my_submit_handler($form, &$form_state, $additional_args){

For some requirement I need to pass additional information to form submit handler. In form api, while defining custom submit handler as

$additional_args = array();
$form['#submit'][] = 'my_submit_handler'

I expect to submit handler as

function my_submit_handler($form, &$form_state, $additional_args){

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

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

发布评论

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

评论(3

浮萍、无处依 2024-09-21 16:00:33

提交处理程序由 drupal fapi 调用,因此您不能执行类似的操作。相反,您可以做的是将您需要的内容添加到 $form$form_state 中。通常的做法是:

  • 在表单中添加一个字段,输入 value 来存储该值。如果表单定义中有该值,请不要执行此操作。

    $form['store'] = array(
      '#type'=> '价值',
      '#value'=> $值
    );
    

    这将在 $form_state['values']['store'] 中提供。

  • 将值添加到 $form_state['storage'],如果您希望将验证句柄中的变量传输到提交处理程序,则完成:

    // 验证。
    $form_state['存储']['值'] = $值;
    
    ...
    
    // 提交
    $value = $form_state['存储']['值'];
    // 当不再使用时需要取消设置存储的值。
    取消设置($form_state['存储']['值']);
    

The submit handler is called by the drupal fapi, so you can't do something like that. Instead what you can do, is to add what you need, either to the $form, or to the $form_state. The usual approaches is to:

  • Added a field to the form, type value to store the value. Don't do this if you have the value in the form definition.

    $form['store'] = array(
      '#type' => 'value',
      '#value' => $value
    );
    

    This will be available in $form_state['values']['store'].

  • Add the value to $form_state['storage'], done if you variables in your validation handle you want to transfer to your submit handler:

    // Validation.
    $form_state['storage']['value'] = $value;
    
    ...
    
    // Submit
    $value = $form_state['storage']['value'];
    // Need to unset stored values when not used anymore.
    unset($form_state['storage']['value']);
    
2024-09-21 16:00:33

Drupal 7:自定义参数通过 $form_state['build_info']['args'] 自动传播
这是在 http://api.drupal.org/api/drupal/ 中说的include!form.inc/function/drupal_get_form/7

例如:

hook_form($form, &$form_state, $myAdditionnalArg) {...}

然后在

hook_form_submit($form, &$form_state) {

...
//$form_state['build_info']['args'] 是一个数组,在索引 0 处包含参数 $myAdditionnalArg 的值
...

Drupal 7: Custom arguments are automatically propagated troug $form_state['build_info']['args']
This is said in http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_get_form/7

Ex:

hook_form($form, &$form_state, $myAdditionnalArg) {...}

Then in

hook_form_submit($form, &$form_state) {

...
//$form_state['build_info']['args'] is an array containing at index 0 the value of argument $myAdditionnalArg
...

岁月静好 2024-09-21 16:00:33

正如 $form['#submit'] 和 $form['#validate'] 和 $ form['#process'] 不再支持自定义参数,将参数传递给提交处理程序集(如所示代码所示)的建议方法是使用类似于以下内容的代码:

$form['#first_paramater'] = $value;
$form['#submit'][] = 'my_submit_handler';

处理程序将检索值作为 <代码>$form['#first_paramater']。
需要注意的是,代码可以使用不同的字符串来代替 #first_paramater,但它必须以 # 开头。

通常不需要像代码那样设置提交处理程序,但在某些情况下是有必要的,例如更改另一个模块创建的表单,或者为表单中存在的每个提交按钮设置不同的提交处理程序。形式。

drupal_retrieve_form() 保存传递的参数到 $form['#parameters'] 中的表单构建处理程序,其中包含:

  • $form_id
  • $form_state参数
  • 传递给表单构建器的

As reported in $form['#submit'] and $form['#validate'] and $form['#process'] no longer support custom parameters, the suggested way to pass parameters to a submission handler set as in the shown code is to use code similar to the following:

$form['#first_paramater'] = $value;
$form['#submit'][] = 'my_submit_handler';

The handler would retrieve the value as $form['#first_paramater'].
To notice that, instead of #first_paramater, the code can use a different string, but it must start with #.

Normally it's not necessary to set a submission handler like the code does, but there are some cases where it is necessary, like to alter a form created by another module, or to set a different submission handler for each of the submission buttons present in a form.

drupal_retrieve_form() saves the parameters passed to the form build handler in $form['#parameters'] which contains:

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