comment_validate

Definition

comment_validate($edit)
drupal/modules/comment/comment.module, line 1257

Description

Validate comment data.

Parameters

$edit An associative array containing the comment data.

Return value

The original $edit.

Code

function comment_validate($edit) {
  global $user;

  // Invoke other validation handlers.
  comment_invoke_comment($edit, 'validate');

  if (isset($edit['date'])) {
    if (strtotime($edit['date']) === FALSE) {
      form_set_error('date', t('You have to specify a valid date.'));
    }
  }
  if (isset($edit['author']) && !$account = user_load(array('name' => $edit['author']))) {
    form_set_error('author', t('You have to specify a valid author.'));
  }

  // Check validity of name, mail and homepage (if given).
  if (!$user->uid || isset($edit['is_anonymous'])) {
    $node = node_load($edit['nid']);
    if (variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) > COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
      if ($edit['name']) {
        $query = db_select('users', 'u');
        $query->addField('u', 'uid', 'uid');
        $taken = $query->where('LOWER(name) = :name', array(':name' => $edit['name']))
          ->countQuery()
          ->execute()
          ->fetchField();
        if ($taken != 0) {
          form_set_error('name', t('The name you used belongs to a registered user.'));
        }
      }
      elseif (variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
        form_set_error('name', t('You have to leave your name.'));
      }

      if ($edit['mail']) {
        if (!valid_email_address($edit['mail'])) {
          form_set_error('mail', t('The e-mail address you specified is not valid.'));
        }
      }
      elseif (variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
        form_set_error('mail', t('You have to leave an e-mail address.'));
      }

      if ($edit['homepage']) {
        if (!valid_url($edit['homepage'], TRUE)) {
          form_set_error('homepage', t('The URL of your homepage is not valid. Remember that it must be fully qualified, i.e. of the form <code>http://example.com/directory</code>.'));
        }
      }
    }
  }

  return $edit;
}