2

Drupal 7.14, Webform 7.x-3.17 : I built a webform that has a hidden field "lt_email", and I'm assigning a session value to it using hook_form_alter -

function wbl_form_webform_client_form_1_alter(&$form, &$form_state) {
   $form['submitted']['lt_email']['#default_value'] = $_SESSION['Some']['Email'];
}

When I var_dump or dpm the ($form) I see the session value for lt_email, but after I hit submit on the webform, the value goes away, it doesn't get assigned.

Since %session[id] didn't handle multi-dimensional session arrays, I used the hook_form_alter workaround. I was under impression hook_form_alter would grab the session value, assign it to hidden field, and once submitted, the hidden field would retain the value, which I then use as the email component for the webform that gets triggered automatically. THe hidden field is blank upon submit.

I'm logged in as root, and all the other fields retain value upon submit (ie, name, address).

3
  • 2
    Try if using #value instead of #default_value gets you a better result.
    – Madis
    Commented May 24, 2013 at 7:27
  • Awesomesauce - that did it, changing #default_value to #value, please add as answer so you may earn proper credit!
    – Robbase
    Commented May 24, 2013 at 14:31
  • Glad it worked :) Just added the comment at first since I wasn't sure whether it's Form API related or Webform does some weird magic there. Wrote up an answer now with added context that should shed some light on what was probably happening.
    – Madis
    Commented May 24, 2013 at 19:16

1 Answer 1

2

The problem is probably caused by the hidden field having the #value property set which overrides #default_value (even if the former is NULL or a empty string).

To get the desired result you have 2 options:

1) Override #value instead of adding a #default_value:

$form['submitted']['lt_email']['#value'] = $_SESSION['Some']['Email'];

2) Unset #value then add #default_value:

unset($form['submitted']['lt_email']['#value']);
$form['submitted']['lt_email']['#default_value'] = $_SESSION['Some']['Email'];

If you are wondering which option is the right one for you then #default_value means that Drupal will accept client-side changes to the field value (via javascript for example). If you don't need/want that possibility then #value should be used (Drupal will ignore any client-side changes in that case). It's mentioned in the documentation as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.