You are here

trim.module in Trim 7

Same filename and directory in other branches
  1. 8 trim.module

File

trim.module
View source
<?php

/**
 * Implements hook_form_alter().
 */
function trim_form_alter(&$form, &$form_state, $form_id) {

  // Ensure that there is an array here.
  if (!isset($form['#validate'])) {
    $form['#validate'] = array();
  }

  // And if someone has set it as a string, fix that issue. You'd be surprised.
  if (!is_array($form['#validate'])) {
    $form['#validate'] = array(
      $form['#validate'],
    );
  }

  // Now add a new function to the list, but ensure that it is called first.
  array_unshift($form['#validate'], 'trim_form_values');
}

/**
 * Validation callback function. Trim the values of the form.
 */
function trim_form_values($form, &$form_state) {
  trim_array_values($form_state['values']);
}

/**
 * Trim all the values of the provided array, and those in all nested arrays.
 *
 * @param array $array
 *   An array of values.
 */
function trim_array_values(&$array) {
  foreach ($array as &$value) {
    if (is_string($value)) {
      $value = trim($value);
    }
    elseif (is_array($value)) {
      trim_array_values($value);
    }
  }
}

Functions

Namesort descending Description
trim_array_values Trim all the values of the provided array, and those in all nested arrays.
trim_form_alter Implements hook_form_alter().
trim_form_values Validation callback function. Trim the values of the form.