0

Using Drupal 7.x. Not much experience with Drupal.

I have a core form in which I need to append some data to a field after it's been submitted by the site visitor. I have found I can alter the submitted data with a form_alter function added to my own custom module. It works fine as far as that, but after the "prepare", $check_cache reverts values.

Is there a way to make this work?

Is there a way to manipulate the "uncacheable keys" to include my item?

Is there a different way to massage submitted data other than hacking the code?

Any other suggestions?

0

Submitted form values are stored in $form_state['values'], and can be modified in a custom validation function.

So in your custom module, implement hook_form_alter() and add a validation function:

/**
 * Implements hook_form_alter().
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'id_of_form_to_alter') {
    $form['#validate'][] = 'mymodule_custom_form_validate';
  }
}

/**
 * Custom form validation handler.
 */
function mymodule_custom_form_validate(&$form, &$form_state) {

  // Append value to submitted field.
  if (!empty($form_state['values']['myfield'])) {
    $form_state['values']['myfield'] .= ' append this string';
  }
}
| improve this answer | |
0

form_set_value() used to change submitted form values during form validation.

Please use form_set_value() instead of using $form_state['values']

Always look for the best practice. Good luck!

| improve this answer | |
  • Just to mention you should make sure there is '&' sign before &$form_state parameter! .. (&$form, &$form_state, $form_id). sometimes developers miss that. – Saud Alfadhli Jul 22 '17 at 23:25

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.