对 Web 表单提交的值使用 Hook_form_alter

发布于 2024-10-31 05:37:48 字数 840 浏览 1 评论 0原文

Drupal 7。Webforms 3.x。

我正在尝试修改提交时的 Web 表单组件值。我制作了一个名为“mos”的自定义模块,并将此代码添加到其中。

function mos_form_alter(&$form, $form_state, $form_id) { 
  if ($form_id == 'webform_client_form_43') {
      dsm($form['#node']->{'webform'}['components']['1']);
      $form['#submit'][] = 'mos_contact_us_submit';
    }
}
function mos_contact_us_submit($form, &$form_state) {
  $form['#node']->{'webform'}['components']['1'] = '[email protected]';
}

但是,当我查看数据库中的结果时,会存储常规的、未覆盖的值。你能帮我知道我做错了什么吗?

最终我想获取输入值并根据提供的内容输出电子邮件地址(例如。24 变成 [电子邮件受保护])但我想我可以自己解决这部分。

Drupal 7. Webforms 3.x.

I am trying to modify a webform component value on submit. I made a custom module called 'mos' and added this code to it.

function mos_form_alter(&$form, $form_state, $form_id) { 
  if ($form_id == 'webform_client_form_43') {
      dsm($form['#node']->{'webform'}['components']['1']);
      $form['#submit'][] = 'mos_contact_us_submit';
    }
}
function mos_contact_us_submit($form, &$form_state) {
  $form['#node']->{'webform'}['components']['1'] = '[email protected]';
}

However when I look at the results in the database the regular, non-overridden value is stored. Can you help let me know what I am doing wrong?

Eventually I want to take the input value and output an email address based on what was provided (for example. 24 turns into [email protected]) But I think I can figure this part out myself.

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

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

发布评论

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

评论(2

甜点 2024-11-07 05:37:48

您应该首先放置您的提交。

array_unshift(
      $form['actions']['submit']['#submit'], 
      'mos_contact_us_submit'
);

但是,如果您想更改 form_state 中的某些变量,则应该使用自定义 _valadate 函数。

You should to place your submit first.

array_unshift(
      $form['actions']['submit']['#submit'], 
      'mos_contact_us_submit'
);

However, if you want to change some variables in form_state, you should to using custom _valadate function.

荒芜了季节 2024-11-07 05:37:48

我得到了它!非常感谢@dobeerman 为我指明了正确的方向。下面是最终运行的代码:

function mos_form_alter(&$form, &$form_state, $form_id) {
  if ('webform_client_form_43' == $form_id) {
    //dsm($form);
    $form['#validate'][] = 'mos_check_email';   
  }
}

function mos_check_email(&$form, &$form_state, $form_id) {
    $emailVal = $form_state['values']['submitted']['to'];
    switch($emailVal) {
        case 1: $emailVal = '[email protected]'; break;
        case 2: $emailVal = '[email protected]'; break;
        case 3: $emailVal = '[email protected]'; break;
                ......
    }
    $form_state['values']['submitted']['to']=$emailVal;
    //dpm($form_state);
}

这样我可以保持电子邮件地址的私密性,但仍然使用 _GET 将变量传递到表单。有点奇怪的情况......但我们正在努力保持一些现有代码完整,所以这似乎是最好的路线。

我不小心弄乱了我的帐户创建,所以我不能给你信用 dobeerman,但我给管理员发了电子邮件,希望我能把它搞清楚,让你得到一些代表!

I got it! BIG Thanks to @dobeerman for pointing me in the right direction. Here is the code that ended up working:

function mos_form_alter(&$form, &$form_state, $form_id) {
  if ('webform_client_form_43' == $form_id) {
    //dsm($form);
    $form['#validate'][] = 'mos_check_email';   
  }
}

function mos_check_email(&$form, &$form_state, $form_id) {
    $emailVal = $form_state['values']['submitted']['to'];
    switch($emailVal) {
        case 1: $emailVal = '[email protected]'; break;
        case 2: $emailVal = '[email protected]'; break;
        case 3: $emailVal = '[email protected]'; break;
                ......
    }
    $form_state['values']['submitted']['to']=$emailVal;
    //dpm($form_state);
}

This way I can keep email address private, but still pass variables to the form with _GET. Kind of a weird situation... but we are trying to keep some existing code intact, so it seemed like the best route.

I accidentally messed up my account creation, so I can't give you the credit dobeerman but I emailed the admins and hopefully I will get it straightened out to get you some rep!

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