You are here

function views_calc_fields_form_validate in Views Calc 7

Same name and namespace in other branches
  1. 5 views_calc.module \views_calc_fields_form_validate()
  2. 6.3 views_calc.module \views_calc_fields_form_validate()
  3. 6 views_calc.module \views_calc_fields_form_validate()

FAPI fields_form validate.

Validate the views calc settings

File

./views_calc.module, line 319
This module will allow you to add calculated fields to views tables and compute (SUM, COUNT, AVG, etc) columns of numeric data in a views table.

Code

function views_calc_fields_form_validate($form, &$form_state) {
  $form_values = $form_state['values'];
  $edit = $form_values;
  foreach ($edit as $delta => $item) {
    if ($item['calc'] == '' || !is_numeric($delta)) {

      // remove blank fields, don't save them
      continue;
    }
    else {

      // Remove all valid values from calc, if anything is left over, it is invalid.
      // First, remove all field names.
      $repl = array();
      $patterns = array();
      $base = $item['base'];
      foreach (_views_calc_substitutions($base) as $key => $value) {
        $key = trim($value);
        $count = strlen($value);
        $replace = preg_quote($value);
        $patterns[] = "`(^|[^\\\\\\\\])" . $replace . "`";
        $repl[] = '${1}';
      }
      $remaining = trim(preg_replace($patterns, $repl, $item['calc']));

      // Next, remove functions and numbers.
      $repl = array();
      $patterns = array();
      foreach (_views_calc_replacements() as $value) {
        $patterns[] = "`(^|[^\\\\\\\\])" . preg_quote(trim($value)) . "`";
        $repl[] = '${1}';
      }
      $remaining = trim(preg_replace($patterns, $repl, $remaining));
      if (!empty($remaining)) {
        form_set_error($form_values[$delta]['calc'], t('The values %remaining in %field are not allowed.', array(
          '%remaining' => $remaining,
          '%field' => $item['label'],
        )));
      }
    }
  }
}