I have created a custom contact form and I have set up an ajax inline validation. My form works very well. But Ajax validation only works when I am connected. When I am not logged in as an administrator, ajax validation does not work.

Also I created a block to display the form in my footer. This form does not appear when I am logged in as an administrator. When I am connected I have access to the form only through the path I defined in module.routing.yml. And when it appears (so when I'm not logged in and I'm just a visitor), Ajax doesn't work on this block form either.

Does anyone have any idea what could cause this? Please help me.


These kind of errors often happen when you have not attached the proper library to your form. So one thing to check is if you have $form['#attached']['library'][] = 'core/drupal.dialog.ajax'; in your form's buildForm().

| improve this answer | |
  • Hi @theuni. Indeed I hadn't attached the library. But I just did, I still have the same problem. – Worksfather Aug 8 '19 at 8:06
  • could you show some of the code? – theuni Aug 8 '19 at 10:45
  • I added the code here stackoverflow.com/questions/57401648/… – Worksfather Aug 8 '19 at 12:02
  • given that the library is now added to the code you postet it looks ok. have you cleared cache? – theuni Aug 8 '19 at 12:20
  • Yes @theuni, I cleared the cache. I was also convinced it would work when I saw your answer. But it still doesn't work. – Worksfather Aug 8 '19 at 12:40

I finally found the solution on the Drupal forum. I put it here, maybe it will help in the future a novice who will be in my case.

What was missing was simply a list of essential javascripts for anonymous users. It seems that at one time it wasn't necessary, but now we have to add them otherwise they wouldn't be loaded for anonymous users.

So what I needed to do was add this to my theme.libraries.yml file

  version: VERSION
    js/scripts.js: {}
    - core/jquery
    - core/drupal.ajax
    - core/drupal
    - core/drupalSettings
    - core/jquery.once
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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