You are here

function _content_admin_field_validate in Content Construction Kit (CCK) 5

Same name and namespace in other branches
  1. 6 includes/content.admin.inc \_content_admin_field_validate()

Validate a field's settings.

File

./content_admin.inc, line 912
Administrative interface for content type creation.

Code

function _content_admin_field_validate($form_id, $form_values, $form) {
  $type = content_types($form_values['type_name']);
  $field = $type['fields'][$form_values['field_name']];
  $field_types = _content_field_types();
  $field_type = $field_types[$field['type']];
  $widget_types = _content_widget_types();
  $widget_type = $widget_types[$field['widget']['type']];
  module_invoke($widget_type['module'], 'widget_settings', 'validate', array_merge($field, $form_values));
  module_invoke($field_type['module'], 'field_settings', 'validate', array_merge($field, $form_values));

  // If content.module is handling the default value,
  // validate the result using the field validation.
  if (content_handle('widget', 'default value', $field) == CONTENT_CALLBACK_DEFAULT) {

    // If this is a programmed form, get rid of the default value widget,
    // we have the default values already.
    if ($form['#programmed']) {
      form_set_value(array(
        '#parents' => array(
          'default_value_widget',
        ),
      ), NULL);
      return;
    }
    if (isset($form_values['default_value_php']) && ($php = trim($form_values['default_value_php']))) {
      ob_start();
      $return = eval($php);
      ob_end_clean();
      if (!is_array($return)) {
        $error = TRUE;
      }
      else {
        foreach ($return as $item) {
          if (!is_array($item)) {
            $error = TRUE;
            break;
          }
        }
      }
      if ($error) {
        $db_info = content_database_info($field);
        $columns = array_keys($db_info['columns']);
        foreach ($columns as $key => $column) {
          $columns[$key] = "'{$column}' => value for {$column}";
        }
        $sample = 'return array(
  0 => array(' . implode(', ', $columns) . '),
  // You\'ll usually want to stop here. Provide more values
  // if you want your \'default value\' to be multi-valued :
  1 => array(' . implode(', ', $columns) . '),
  2 => ...
);';
        form_set_error('default_value_php', t('The default value PHP code returned an incorrect value<br/>Expected format : <pre>!sample</pre>Returned value : @value', array(
          '!sample' => $sample,
          '@value' => print_r($return, true),
        )));
        return;
      }
      else {
        $default_value = $return;
        $is_code = TRUE;
        form_set_value(array(
          '#parents' => array(
            'default_value_php',
          ),
        ), $php);
        form_set_value(array(
          '#parents' => array(
            'default_value',
          ),
        ), array());
      }
    }
    else {
      $default_value = $form_values['default_value_widget'][$field['field_name']];
      $is_code = FALSE;
      form_set_value(array(
        '#parents' => array(
          'default_value_php',
        ),
      ), '');
      form_set_value(array(
        '#parents' => array(
          'default_value',
        ),
      ), $default_value);
    }
    if (isset($default_value)) {
      $node = array();
      $node[$form_values['field_name']] = $default_value;
      $field['required'] = FALSE;
      $field_function = $field_type['module'] . '_field';
      $widget_function = $widget_type['module'] . '_widget';

      // If default_value is created from PHP code, don't run it through widget processing.
      // This way the default value code can directly create the correct array without being
      // mangled by widget processing which sometimes requires an input array in a completely different format.
      if (function_exists($widget_function) && !$is_code) {
        $widget_function('validate', $node, $field, $default_value);
        $widget_function('process form values', $node, $field, $default_value);

        // The widget processing may have altered the value, save it to be sure.
        form_set_value(array(
          '#parents' => array(
            'default_value',
          ),
        ), $default_value);
      }
      if (function_exists($field_function)) {
        $field_function('validate', $node, $field, $default_value, NULL, NULL);
      }

      // The field validation routine won't set an error on the right field, so set it here.
      if (form_get_errors()) {
        if (trim($form_values['default_value_php'])) {
          form_set_error('default_value_php', t('The default value PHP code created @value which is invalid.', array(
            '@value' => print_r($default_value, true),
          )));
        }
        else {
          form_set_error('default_value', t('The default value is invalid.'));
        }
      }
    }
  }
}