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

Perform validation checks on the given node.

3 calls to node_validate()
blogapi_blogger_edit_post in modules/blogapi/blogapi.module
Blogging API callback. Modifies the specified blog node.
blogapi_blogger_new_post in modules/blogapi/blogapi.module
Blogging API callback. Inserts a new blog post as a node.
node_form_validate in modules/node/node.pages.inc

File

modules/node/node.module, line 789
The core that allows content to be submitted to the site. Modules and scripts may programmatically submit nodes using the usual form API pattern.

Code

function node_validate($node, $form = array()) {

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

  // Make sure the body has the minimum number of words.
  // TODO : use a better word counting algorithm that will work in other languages
  if (!empty($type->min_word_count) && isset($node->body) && count(explode(' ', $node->body)) < $type->min_word_count) {
    form_set_error('body', t('The @body_label of your @type is too short. You need at least %words words.', array(
      '@body_label' => $type->body_label,
      '@type' => $type->name,
      '%words' => $type->min_word_count,
    )));
  }
  if (isset($node->nid) && node_last_changed($node->nid) > $node->changed) {
    form_set_error('changed', t('This content has been modified by another user, changes cannot be saved.'));
  }
  if (user_access('administer nodes')) {

    // Validate the "authored by" field.
    if (!empty($node->name) && !($account = user_load(array(
      'name' => $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.
      form_set_error('name', t('The username %name does not exist.', array(
        '%name' => $node->name,
      )));
    }

    // Validate the "authored on" field. As of PHP 5.1.0, strtotime returns FALSE instead of -1 upon failure.
    if (!empty($node->date) && strtotime($node->date) <= 0) {
      form_set_error('date', t('You have to specify a valid date.'));
    }
  }

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