2

Drupal 8.9 and paragraph 8.x-1.11

My issue is that Ajax breaks when altering the #validate form. This is an issue for any form! It may have to do with my ajax, but here is my setup.

I have a paragraph type:

contact_information

  • field_phone_number (telephone from core)
  • field_type_of_phone (text_plain)

I have a custom module. In this custom module I am creating a form_alter on /user/register

I have:

function telephone_alter_form_user_register_form_alter(&$form, $form_state, $form_id ){
  //$form['#validate'] = array();  //commenting this in or out doesn't help
  $form['#validate'][] = 'telephone_alter_test_validate';
  //$form['field_phone']['widget']['#element_validate'][] = 'telephone_alter_test_validate';
}

function telephone_alter_test_validate($form, &$form_state) {
   echo "hi"; //prints hi, but ajax breaks
}

The validation works, prints out debugging. However, when I try to add another paragraph element, it does nothing. This is for any form. I have tried this on other forms like adding a basic page and trying to call an alter function!

the Ajax error displayed is:

[{"command":"insert","method":"prepend","selector":null,"data":"\u003Cdiv data-drupal-messages\u003E\n  \u003Cdiv class=\u0022messages__wrapper\u0022\u003E\n \u003Cdiv class=\u0022alert alert-danger alert-dismissible\u0022 role=\u0022alert\u0022 aria-label=\u0022Error message\u0022\u003E\n \u003Cbutton type=\u0022button\u0022 role=\u0022button\u0022 class=\u0022close\u0022 data-dismiss=\u0022alert\u0022 aria-label=\u0022Close\u0022\u003E\u003Cspan aria-hidden=\u0022true\u0022\u003E\u0026times;\u003C\/span\u003E\u003C\/button\u003E\n \u003Ch2 class=\u0022sr-only\u0022\u003EError message\u003C\/h2\u003E\n \u003Cp\u003EAn unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (40 MB) that this server supports.\u003C\/p\u003E\n \u003C\/div\u003E\n    \u003C\/div\u003E\n\u003C\/div\u003E\n","settings":null}]

The Ajax error is key, because if I create an alter form for any other page, I still get the error.

Any help is great! Thank you! I can provide more info if needed

2 Answers 2

1

I had the same issue. It turned out every time a paragraph is added the validation method runs. In your case, echo is illegal and it stops the following process. Try replacing it with php error_log or Devel debug functions.

2
  • 3
    After finding out about Xdebug, life has been so much simpler. I would almost say having Xdebug is required to break and debug on ajax calls. Commented Dec 10, 2020 at 5:32
  • Also Devel Debug Log module can debug ajax callbacks.
    – 863
    Commented Dec 21, 2020 at 3:10
0

If you "initialize" the #validate form array key, you will loose the validations set by other modules (like paragraphs). This is bad practice and can break things.

Add your custom validation method like this:

function telephone_alter_form_user_register_form_alter(&$form, $form_state, $form_id ){
  $form['#validate'][] = 'telephone_alter_test_validate';
}
1
  • I removed the initialization to no avail :( here is what kint::dump($form['#validate']); prints after I add my function. 0 => string (14) "::validateForm" newline 1 => string (29) "telephone_alter_test_validate" . Ajax still break and I can't add more paragraphs. Commented Aug 7, 2020 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.