comment_form_validate

Versions
4.7 – 5
comment_form_validate($form_id, $form_values)
6 – 7
comment_form_validate($form, &$form_state)

Validate comment form submissions.

Code

modules/comment/comment.module, line 1976

<?php
function comment_form_validate($form, &$form_state) {
  global $user;
  $comment = (object) $form_state['values'];
  field_attach_form_validate('comment', $comment, $form, $form_state);

  if (isset($form_state['values']['date'])) {
    if (strtotime($form_state['values']['date']) === FALSE) {
      form_set_error('date', t('You have to specify a valid date.'));
    }
  }
  if (isset($form_state['values']['author']) && !$account = user_load_by_name($form_state['values']['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($form_state['values']['is_anonymous'])) {
    $node = node_load($form_state['values']['nid']);
    if (variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) > COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
      if ($form_state['values']['name']) {
        $query = db_select('users', 'u');
        $query->addField('u', 'uid', 'uid');
        $taken = $query
          ->where('LOWER(name) = :name', array(':name' => $form_state['values']['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 ($form_state['values']['mail']) {
        if (!valid_email_address($form_state['values']['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 ($form_state['values']['homepage']) {
        if (!valid_url($form_state['values']['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>.'));
        }
      }
    }
  }
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.