Overrides Drupal\Core\Entity\EntityFormController::validate().
Overrides EntityFormController::validate
public function validate(array $form, array &$form_state) {
parent::validate($form, $form_state);
if (!empty($form_state['values']['cid'])) {
// Verify the name in case it is being changed from being anonymous.
$account = user_load_by_name($form_state['values']['name']);
$form_state['values']['uid'] = $account ? $account->uid : 0;
$date = new DrupalDateTime($form_state['values']['date']);
if ($date
->hasErrors()) {
form_set_error('date', t('You have to specify a valid date.'));
}
if ($form_state['values']['name'] && !$form_state['values']['is_anonymous'] && !$account) {
form_set_error('name', t('You have to specify a valid author.'));
}
}
elseif ($form_state['values']['is_anonymous']) {
// Validate anonymous comment author fields (if given). If the (original)
// author of this comment was an anonymous user, verify that no registered
// user with this name exists.
if ($form_state['values']['name']) {
$query = db_select('users', 'u');
$query
->addField('u', 'uid', 'uid');
$taken = $query
->condition('name', db_like($form_state['values']['name']), 'LIKE')
->countQuery()
->execute()
->fetchField();
if ($taken) {
form_set_error('name', t('The name you used belongs to a registered user.'));
}
}
}
}