You are here

function _webform_submit_component in Webform 7.4

Same name and namespace in other branches
  1. 6.3 webform.api.php \_webform_submit_component()
  2. 7.3 webform.api.php \_webform_submit_component()

A hook for changing the input values before saving to the database.

Webform expects a component to consist of a single field, or a single array of fields. If you have a component that requires a deeper form tree you must flatten the data into a single array using this callback or by setting #parents on each field to avoid data loss and/or unexpected behavior.

Note that Webform will save the result of this function directly into the database.

Parameters

$component: A Webform component array.

$value: The POST data associated with the user input.

Return value

array An array of values to be saved into the database. Note that this should be a numerically keyed array.

Related topics

File

./webform.api.php, line 1103
Sample hooks demonstrating usage in Webform.

Code

function _webform_submit_component($component, $value) {

  // Clean up a phone number into 123-456-7890 format.
  if ($component['extra']['phone_number']) {
    $number = preg_replace('/[^0-9]/', '', $value[0]);
    if (strlen($number) == 7) {
      $number = substr($number, 0, 3) . '-' . substr($number, 3, 4);
    }
    else {
      $number = substr($number, 0, 3) . '-' . substr($number, 3, 3) . '-' . substr($number, 6, 4);
    }
  }
  $value[0] = $number;
  return $value;
}