0

drupal 7 webform 7-3.18

My webform has a hidden field used to store an unique id. This id is generate from custom logic.

my code is to set this field value. 'webform_client_form_1' is my webform id

function webform_custom_form_webform_client_form_1_alter($form, $form_state, $form_id) {
  $form['submitted']['test_id']['#value'] = 'COURSE_ID-123456';      
}

When the form is submitted and view the submission result and this hidden field value is empty

2

Ok, try not to confuse Webform module stuff with core drupal FAPI stuff.

You cannot set $form['submitted']['test_id']['#value'] = 'COURSE_ID-123456'; in the form_alter because $form is not passed in by reference and the submitted key values is set after the form is POSTed to my knowledge.

So you're trying to put stuff into the form in the wrong time and place. If I was just using FAPI and not webform. I would not use a "hidden" form value (which isnt really hidden on the page), but use Form build_args or $form_state['MYMODULE_temporary'] to hold my custom course_id in the FAPI form lifecycle so that the client never sees these values, but they are passed along through form validation and submission.

You can probably do the same technique I describe above and then implement hook_webform_submission_insert(), or hook_webform_submission_update() (if your form supports Drafts) to perform submission logic and look at your custom course_id stuff ...

  • Thankyou for directing me to webform api. I just read the api why not using hook_webform_submission_presave() ? I tried to dprint_r the $submission variable the see the data structure but nothing outputted. – Adrian Mar 31 '13 at 3:50
  • I dont use _presave() ever because if you allow Drafts the form is saved many time(s); the 1st time is via an _insert() but the submission is still a Draft ... ultimate the 1st "final" save of a Draftable webform is on an hook_webform_submission_update() ... so depending on what you're actually doing, you need to figure out the correct API call that works with your desired logic. – tenken Mar 31 '13 at 5:28
0

For my webform alterations I use a rules module, Rules Forms Support. An example is that on form build I verify the user role and set a form element to hidden, so I have a field in the form only editable based on user role. I know there is also a webform rules module, but I have found the Rules Forms Support module to be more robust and have greater application than just modifying webforms. Rule actions can be used and even run or call the custom code to set the value at the proper step.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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