I’m working on a Symfony blog application and I have two separate modules: one for articles and another for comments. The comment system is fully built with moderation features and backend functionality.
My goal is to display the comment submission form on the article detail page. I tried copying the form template from the comment module’s success page but it doesn’t work properly even with some configuration changes.
Is there a proper way to embed a form from the comment module directly into the article module’s view? What configuration steps are needed to make this work correctly?
I have all the permissions and user groups set up already. I just need to figure out the right approach to share forms between modules in Symfony.
Any guidance would be helpful. Thanks for taking the time to help.
another option is using symfony’s service container - create a form service in your comment bundle and inject it into the article controller. then you can build the form there and pass it to your template. this way you dont need embedded controllers and have more control over form handling.
hmm interesting approaches above! im curious tho - have you considered using symfony’s fragment system? i’ve seen some devs struggle with form token validation when embeding forms between modules. are you running into any csrf issues with your current setup? also wondering what version of symfony you’re working with since the aproach might vary slightly?
I encountered a similar situation when building a news site with separate content and interaction modules. The cleanest approach I found was using Symfony’s embedded controllers feature. Create a controller action in your comment module that renders just the form, then use the render() function in your article template to call this controller. In your article detail template, add something like {{ render(controller('CommentBundle:Comment:renderForm', {'article_id': article.id})) }}
. This maintains proper separation of concerns and ensures all form handling logic stays within the comment module. You’ll need to make sure your comment form controller can accept the article ID parameter and properly initialize the form with the correct article reference. This approach has worked reliably for me across multiple projects and keeps the modules properly decoupled while sharing functionality.