1 node.module node_validate($node, $form, &$form_state)

Performs validation checks on the given node.

See also

node_form_validate()

File

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

Code

function node_validate($node, $form, &$form_state) {
  if (isset($node->nid) && (node_last_changed($node->nid) > $node->changed)) {
    form_set_error('changed', t('The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.'));
  }

  // Validate the "authored by" field.
  if (!empty($node->name) && !($account = user_load_by_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.
  if (!empty($node->date)) {
    $node_date = $node->date;
    if (is_array($node->date)) {
      $node_date = implode(' ', $node->date);
    }
    if (strtotime($node_date) === FALSE) {
      form_set_error('date', t('You have to specify a valid date.'));
    }
  }

  // Validate the "Publish on" date.
  if ($node->status == NODE_SCHEDULED && !empty($node->scheduled)) {
    $node_scheduled = $node->scheduled;
    if (is_array($node->scheduled)) {
      $node_scheduled = implode(' ', $node->scheduled);
    }
    // Validate the entire string is valid.
    if (strtotime($node_scheduled) === FALSE) {
      form_set_error('scheduled', t('You have to specify a valid date.'));
    }
    // Validate the date/time is not in the past.
    else {
      $scheduled_date = new DateTime($node_scheduled, date_default_timezone_object());
      if ($scheduled_date && $scheduled_date->format('U') < REQUEST_TIME) {
        form_set_error('scheduled', t('Scheduled publish time cannot be in the past.'));
      }
    }
  }

  // Invoke hook_validate() for node type specific validation and
  // hook_node_validate() for miscellaneous validation needed by modules. Can't
  // use node_invoke() or module_invoke_all(), because $form_state must be
  // receivable by reference.
  $function = node_type_get_base($node) . '_validate';
  if (function_exists($function)) {
    $function($node, $form, $form_state);
  }
  foreach (module_implements('node_validate') as $module) {
    $function = $module . '_node_validate';
    $function($node, $form, $form_state);
  }
}