Same name and namespace in other branches
  1. 4.7.x modules/node.module \node_validate()
  2. 5.x modules/node/node.module \node_validate()
  3. 6.x modules/node/node.module \node_validate()
  4. 7.x modules/node/node.module \node_validate()

Perform validation checks on the given node.

4 calls to node_validate()
blogapi_blogger_edit_post in modules/blogapi.module
Blogging API callback. Modifies the specified blog node.
blogapi_blogger_new_post in modules/blogapi.module
Blogging API callback. Inserts a new blog post as a node.
node_form in modules/node.module
Generate the node editing form.
node_submit in modules/node.module
Accepts a submission of new or changed node content.

File

modules/node.module, line 1214
The core that allows content to be submitted to the site.

Code

function node_validate($node) {
  global $user;

  // Convert the node to an object, if necessary.
  $node = array2object($node);

  // Validate the title field.
  if (isset($node->title)) {
    if (trim($node->title) == '') {
      form_set_error('title', t('You have to specify a title.'));
    }
  }

  // Make sure the body has the minimum number of words.
  // todo use a better word counting algorithm that will work in other languages
  if (isset($node->body) && count(explode(' ', $node->body)) < variable_get('minimum_' . $node->type . '_size', 0)) {
    form_set_error('body', t('The body of your %type is too short. You need at least %words words.', array(
      '%words' => variable_get('minimum_' . $node->type . '_size', 0),
      '%type' => node_invoke($node->type, 'node_name'),
    )));
  }

  // Auto-generate the teaser, but only if it hasn't been set (e.g. by a
  // module-provided 'teaser' form item).
  if (!isset($node->teaser)) {
    $node->teaser = node_teaser($node->body, $node->format);
  }
  if (node_last_changed($node->nid) > $node->changed) {
    form_set_error('changed', t('This content has been modified by another user, unable to save changes.'));
  }
  if (user_access('administer nodes')) {

    // Set up default values, if required.
    if (!$node->created) {
      $node->created = time();
    }
    if (!$node->date) {
      $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
    }

    // Validate the "authored by" field.
    if (empty($node->name)) {

      // The use of empty() is mandatory in the context of usernames
      // as the empty string denotes the anonymous user.  In case we
      // are dealing with an anonymous user we set the user ID to 0.
      $node->uid = 0;
    }
    else {
      if ($account = user_load(array(
        'name' => $node->name,
      ))) {
        $node->uid = $account->uid;
      }
      else {
        form_set_error('name', t('The username %name does not exist.', array(
          '%name' => theme('placeholder', $node->name),
        )));
      }
    }

    // Validate the "authored on" field.
    if (strtotime($node->date) != -1) {
      $node->created = strtotime($node->date);
    }
    else {
      form_set_error('date', t('You have to specify a valid date.'));
    }
  }
  else {

    // Validate for normal users:
    $node->uid = $user->uid ? $user->uid : 0;

    // Force defaults in case people modify the form:
    $node_options = variable_get('node_options_' . $node->type, array(
      'status',
      'promote',
    ));
    $node->status = in_array('status', $node_options);
    $node->moderate = in_array('moderate', $node_options);
    $node->promote = in_array('promote', $node_options);
    $node->sticky = in_array('sticky', $node_options);
    $node->revision = in_array('revision', $node_options);
    unset($node->created);
  }

  // Create a new revision when required.
  $node = node_revision_create($node);

  // Do node-type-specific validation checks.
  node_invoke($node, 'validate');
  node_invoke_nodeapi($node, 'validate');

  // Check input format access
  if (array_key_exists('format', $node) && !filter_access($node->format)) {
    form_set_error('format', t('The supplied input format is invalid.'));
  }
  $node->validated = TRUE;
  return $node;
}