Answer the question
In order to leave comments, you need to log in
What can be wrong with saving a comment in Drupal 8?
The comment-form form has been created in the admin panel. In mytheme.theme I change the form:
function mytheme_form_comment_comment_form_alter(&$form, FormStateInterface $form_state, $form_id = NULL) {
$form['actions']['submit']['#validate'] = ['Drupal\mytheme\AjaxCommentForm::ajaxValidateCallback'];
$form['actions']['submit']['#ajax'] = [
'callback' => 'Drupal\mytheme\AjaxCommentForm::ajaxSubmitCallback',
'event' => 'click',
'progress' => FALSE,
];
}
class AjaxCommentForm {
public function ajaxSubmitCallback(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
...
$message = [
'#theme' => 'status_messages',
'#message_list' => \Drupal::service('messenger')->all(),
'#status_headings' => [
'status' => t('Status message'),
'error' => t('Error message'),
'warning' => t('Warning message'),
],
];
$messages = \Drupal::service('renderer')->render($message);
$response->addCommand(new HtmlCommand('#' . Html::getClass($form['form_id']['#value']) . '-messages', $messages));
return $response;
}
public function ajaxValidateCallback(array &$form, FormStateInterface $form_state) {
...
if (strlen($form_state->getValue('field_comment_name')) === 0) {
$form_state->setErrorByName('field_comment_name', 'Введите Ваше имя');
}
}
}
Drupal\Core\Entity\EntityStorageException: Entity validation was skipped.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question