You are here

function views_edit_view_validate in Views (for Drupal 7) 5

Validate that a view sent via form is OK.

File

./views_ui.module, line 1744

Code

function views_edit_view_validate($form_id, $view, $form) {
  $op = $view['op'];
  if ($op != t('Save') && $op != t('Save and edit')) {
    return;

    // only validate on saving!
  }
  if ($view['vid']) {
    $changed = db_result(db_query("SELECT changed FROM {view_view} WHERE vid = %d", $view['vid']));
    if ($changed && $view['changed'] != $changed) {
      form_set_error('', t('Another user has modified this view, unable to save. You can get this error by using the Back button to re-edit a view after saving one; if you do this, be sure to Reload before making any changes!'));
      return;
    }
  }
  if (!$view['name']) {
    form_error($form['basic-info']['name'], t('View name is required.'));
  }

  // view name must be alphanumeric or underscores, no other punctuation.
  if (preg_match('/[^a-zA-Z0-9_]/', $view['name'])) {
    form_error($form['basic-info']['name'], t('View name must be alphanumeric or underscores only.'));
  }

  // test uniqueness of name
  $vid = db_result(db_query("SELECT vid FROM {view_view} WHERE name='%s'", $view['name']));
  if ($vid && $vid != $view['vid']) {
    form_error($form['basic-info']['name'], t('View name already in use.'));
  }
  if ($view['page']) {
    if ($view['use_pager'] && !$view['nodes_per_page']) {
      form_error($form['page-info']['nodes_per_page'], t('Nodes per page cannot be 0 if using the pager.'));
    }
  }
  if ($view['block'] && $view['nodes_per_block'] < 1) {
    form_error($form['block-info']['nodes_per_block'], t('If being used as a block, Nodes Per Block must be positive.'));
  }

  // validation based on type:
  $plugins = _views_get_style_plugins();
  if ($view['page']) {
    $validator = $plugins[$view['page_type']]['validate'];
    if (function_exists($validator)) {
      $validator('page', $view, $form);
    }
  }
  if ($view['block']) {
    $validator = $plugins[$view['block_type']]['validate'];
    if (function_exists($validator)) {
      $validator('block', $view, $form);
    }
  }
  foreach (array(
    'field',
    'argument',
    'sort',
    'filter',
  ) as $type) {
    $function = "_views_get_{$type}" . 's';
    $info = $function();
    if (is_array($view[$type])) {
      foreach ($view[$type] as $key => $data) {
        if (!is_numeric($key)) {
          continue;

          // some non-data data is in here.
        }
        $validator = $info[$data['id']]['validate'];
        if (function_exists($validator)) {
          $validator($data, $view, $form);
        }
      }
    }
  }
}