I'm trying to get Symfony Forms (with Twig Bridge), Slim and Twig playing nicely together but can't see to do it with any success. From what I can see I have the form aspect of things working, in that the form is created, the createView()
method does as it should and I can offset it into a Twig template and use {{ dump(form) }}
to see that everything is going as it should from there.
I have a factor which creates the relevant stuff for rendering forms in Twig and set s the default template etc - all of which seems to be working, but for some reason when I ret and render the form, I get the following error:
Type: Twig_Error_Syntax
Message: Unknown "rootform" test.
File: /var/www/mulligan/vendor/symfony/twig-bridge/Resources/views/Form/form_div_layout.html.twig
Line: 22
I've checked to make sure the FormExtension is loading and it is, and it's definining the test functions etc (one of which is rootform) and all seems to be working fine. This is basically the Factor I'm using to define the FormFactory (and handle the Twig stuff as necessary too):
/**
* @return FormFactoryInterface|null
* @throws \ReflectionException
*/
public function initialiseFactory(): ?FormFactoryInterface
{
$csrfGenerator = new UriSafeTokenGenerator();
$csrfStorage = new NativeSessionTokenStorage();
$csrfManager = new CsrfTokenManager($csrfGenerator, $csrfStorage);
$defaultFormTheme = 'form_div_layout.html.twig';
$appVariableReflection = new ReflectionClass(AppVariable::class);
$vendorTwigBridgeDirectory = \dirname($appVariableReflection->getFileName());
$viewsDirectory = PROJECT_ROOT . '/templates/form';
$twig = $this->getTwigView();
$twigEnv = new Environment(new FilesystemLoader([
$viewsDirectory,
$vendorTwigBridgeDirectory . '/Resources/views/Form'
]));
$formEngine = new TwigRendererEngine([$defaultFormTheme], $twigEnv);
$twig->getEnvironment()->addRuntimeLoader(new FactoryRuntimeLoader([
FormRenderer::class => function () use ($formEngine, $csrfManager) {
return new FormRenderer($formEngine, $csrfManager);
}
]));
$twig->addExtension(new FormExtension());
$validator = Validation::createValidator();
// Set up the Form component
$formFactory = Forms::createFormFactoryBuilder()
->addExtension(new HttpFoundationExtension())
->addExtension(new CsrfExtension($csrfManager))
->addExtension(new ValidatorExtension($validator))
->getFormFactory();
$this->setFactory($formFactory);
return $this->getFactory();
}
Has anyone got any thoughts on what I may be missing, or anything which may be failing to initialise whilst doing this - I wouldn't normally use Symfony Forms etc, so unsure where the issue may lie.
NB: composer.json for versions etc:
"slim/slim": "^3.11",
"slim/php-view": "2.1.*",
"symfony/form": "^4.2",
"symfony/security-csrf": "^4.2",
"symfony/twig-bridge": "^4.2",
"symfony/validator": "^4.2",
"symfony/http-foundation": "^4.2",
"slim/twig-view": "^2.4"
thanks in advance