1

I need to implement a form inside my show template of a specific admin. In the original demo of Symfony a great example is shown. Please take a look at it here;

  1. Controller (look at the method on line 120 as well)
  2. Post Show template
  3. Comment Form template

Now what I've done in Sonata;

I've created the custom CRUD Controller for the specific admin class (OrderAdmin). I already have a clone action there that works well, so I know my custom CRUD Controller works perfect.

I also have a custom show template specific for OrderAdmin. Again that works well, because I already have something custom there.

Now for my work relating to this custom comment form;

My action just to render the form (like in the Symfony demo):

/**
 * @param Order $order
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function commentFormAction(Order $order)
{
    $form = $this->createFormBuilder()
        ->add('comment')
        ->getForm();

    return $this->render('AppBundle:Order:_comment_form.html.twig', array(
        'order' => $order,
        'form' => $form->createView(),
    ));
}

A snippet of my show template:

<div class="row">
        <div class="col-md-12">
            {{ render(controller('AppBundle:OrderCRUD:commentForm', { 'id': object.id })) }}
        </div>
    </div>

and finally my _comment_form template where the form self is rendered:

{{ form_start(form) }}
    {{ form_widget(form) }}
    <button class="btn btn-primary pull-right" type="submit">Comment</button>
{{ form_end(form) }}

I'm pretty sure this is all setup fine, the only problem I get is that...

An exception has been thrown during the rendering of a template ("There is no _sonata_admin defined for the controller AppBundle\Controller\OrderCRUDController and the current route ``").

I have another action that will be the comment form action, but I get the problem here before that action is called on form submission.

Can somebody please help me understand this _sonata_admin. How do I define it? What should I had to my code, or how do I have to change it to get a form function properly in my show template. Does anybody have an example of how to accomplish this? I've come across a few posts over the web where people struggle with this.

I've also started a bounty on this question with a very very similar issue.

I desperately need help to get this to work. Please? I know Sonata support is quite poor but I need you guys, please.

Community
  • 1
  • 1
Mentos93
  • 581
  • 1
  • 7
  • 28
  • You should debug [this line](https://github.com/sonata-project/SonataAdminBundle/blob/240ecbd02d453f9acaf81e00112ff5d8dae4459b/Route/RouteCollection.php#L87), see if its executed for your custom route – greg0ire Nov 07 '16 at 11:04
  • Ok, and what if it's not executed? What do I have to do then? – Mentos93 Nov 07 '16 at 12:28
  • I'd check the `configureRoutes` method of the admin. The method is supposed to be called here. – greg0ire Nov 07 '16 at 12:58
  • After googling on '_sonata_admin' the past week or two, I just 'accidentally' come across this line: `{{ render(controller("XxxxBundle:XxxxCRUS:xxxx", {'xx': object.xx, '_sonata_admin' : 'sonata.admin.xxxx'})) }}`. It seems like it fixed it! Now how am I suppose to know about this? – Mentos93 Nov 07 '16 at 13:28
  • This looks more like a workaround. I think I already wrote custom actions myself and didn't need to do that. – greg0ire Nov 07 '16 at 13:33
  • IMO the problem has to do with the only thing you don't show : the routing part of your problem. See the docs : https://sonata-project.org/bundles/admin/3-x/doc/cookbook/recipe_custom_action.html#bringing-it-all-together – greg0ire Nov 07 '16 at 13:34
  • I have another custom action, also a clone action, and with that one I have no problem whatsoever. I've followed that cookbook and it worked. But now with this action it doesn't want to work, I keep on getting the mentioned error. I have the route configured in `configuredRoutes()`, and if I modify the URL accordingly it works, no problem with that. But the issue is that the action doesn't want to render in my show template with this `{{ render(controller('AppBundle:OrderCRUD:comment')) }}`. And then if I add this part `{ '_sonata_admin': 'sonata.admin.pmod_order' }` does work. – Mentos93 Nov 07 '16 at 15:29
  • Oh right, and there is no routing involved at this point. The routing provides this argument. Maybe make a PR to document that? – greg0ire Nov 07 '16 at 15:32
  • Ok, will try that, still pretty new to all this, because of that the faulty issue on github (should have payed more attention to the rules). – Mentos93 Nov 07 '16 at 15:42
  • So I guess the best way to render an action is to do this 'workaround' by adding `{ '_sonata_admin': 'sonata.admin.pmod_order' }`? – Mentos93 Nov 07 '16 at 15:48
  • I think so too… – greg0ire Nov 07 '16 at 15:52
  • 1
    Thank you very much for your support, greg0ire. I appreciate it a lot. – Mentos93 Nov 07 '16 at 16:10

0 Answers0