1

I'm trying to rewrite a "custom framework" application to the Symfony, but I can not do everything at once, so I've divided the process into steps.

From important notes - I've already implemented the symfony/templating component and the symfony/twig-bridge component.

That's how I want to output the form in the template:

<?php echo $view['form']->form($form) ?>

As I'm doing so the following error is thrown:

Symfony\Component\Form\Exception\LogicException
No block "form" found while rendering the form.
/var/www/html/vendor/symfony/form/FormRenderer.php on line 98

To render the templates I'm using the DelegatingEngine which uses the PhpEngine and the TwigEngine.

Setting up the Twig with the \Symfony\Bridge\Twig\Extension\FormExtension is well documented, but what I'm missing is the php setup. This is how I'm doing this:

new \Symfony\Component\Form\Extension\Templating\TemplatingExtension($phpEngine, $this->csrfManager());

Could you point me what am I missing or what's wrong with my setup?

2 Answers 2

1

I think the simplest way would have been to install the Symfony 3.3 standard edition next to your app (pending the release of Symfony Flex).

After this, find a way to use the router of Symfony with the router of your application.

So you could have the full Symfony framework, create your form type in it and let Symfony render it :

  1. With an ajax call
  2. With a new Symfony Kernel in your legacy app
Sign up to request clarification or add additional context in comments.

3 Comments

that's a nice idea but it'd involve a mess in the project :/
it also doesn't answer the question. I'll try and look at your proposed solution as I am stuck on this pohole in my freedom's adventure into creating Symfony Forms within Standalone context. I'll report back on this, interesting read.
@roomcayz Also I'd suggest updating the answer with namespace references at least. I think it could be much more useful to know where your dependencies come from.
1

I've found the answer:

I was using the wrong FormRendererEngineInterface. Instead of relying on the \Symfony\Component\Form\Extension\Templating\TemplatingExtension class I've registered the form helper by myself:

$phpEngine = new PhpEngine(new TemplateNameParser(), new FilesystemLoader(realpath(__DIR__.'/../Template').'/%name%'));
$twigEngine = new TwigEngine($this->twig(), new TemplateNameParser());

$this->TemplateEngine = new DelegatingEngine(array(
    $phpEngine,
    $twigEngine,
));

$phpEngine->addHelpers(array(
    new FormHelper(new FormRenderer($this->twigFormRendererEngine())),
));

As you can see in the TemplatingEngine:

public function __construct(PhpEngine $engine, CsrfTokenManagerInterface $csrfTokenManager = null, array $defaultThemes = array())
    {
        $engine->addHelpers(array(
            new FormHelper(new FormRenderer(new TemplatingRendererEngine($engine, $defaultThemes), $csrfTokenManager)),
        ));
    }

It relies on the TemplatingRendererEngine while I need the TwigRendererEngine instance, as the form templates are the twig files.

Correct me if my explanation is wrong, but the solution is working.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.