I have the problem with Twig and Symfony Form component. I have integrated Form and Twig in my project (I use PHP 5.5.9, no update planned, so I use all Symfony required component in 3.4.4 version)
I follow this guide: https://symfony.com/doc/3.4/components/form.html
On page rendering I get the following error:
Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "trans" filter.' in /symfony/twig-bridge/Resources/views/Form/bootstrap_4_layout.html.twig:158 Stack trace
Here is my code:
Use section:
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage;
use Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Validator\Validation;
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Component\Translation\Translator;
Code used to render the form:
$defaultFormTheme = 'bootstrap_4_layout.html.twig';
$appVariableReflection = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
$vendorTwigBridgeDirectory = dirname($appVariableReflection->getFileName());
$viewsDirectory = realpath(__DIR__."/../../app/template/html");
$csrfGenerator = new UriSafeTokenGenerator();
$csrfStorage = new SessionTokenStorage($this->session);
$csrfManager = new CsrfTokenManager($csrfGenerator, $csrfStorage);
$validator = Validation::createValidator();
// Set up the Translation component
$translator = new Translator('it');
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource('xlf', $this->libDir.'/symfony/form/Resources/translations/validators.it.xlf', 'it', 'validators');
$translator->addResource('xlf', $this->libDir.'/symfony/validator/Resources/translations/validators.it.xlf', 'it', 'validators');
$loader = new \Twig_Loader_Filesystem(array($viewsDirectory,$vendorTwigBridgeDirectory.'/Resources/views/Form'));
$twig = new \Twig_Environment($loader,array(
'cache' => realpath(__DIR__."/../../app/template/cache"),
'debug' => true,
));
$twig->addExtension(new FormExtension());
$formEngine = new TwigRendererEngine(array($defaultFormTheme), $twig);
$twig->addRuntimeLoader(new \Twig_FactoryRuntimeLoader(array(
FormRenderer::class => function () use ($formEngine,$csrfManager) {
return new FormRenderer($formEngine,$csrfManager);
},
)));
$formFactory = Forms::createFormFactoryBuilder()
->addExtension(new HttpFoundationExtension())
->addExtension(new CsrfExtension($csrfManager))
->addExtension(new ValidatorExtension($validator))
->getFormFactory();
$form = $formFactory->createBuilder(FormType::class, null, array(
'action' => 'index.php',
'method' => 'POST'
))
->add('username',TextType::class,array(
'constraints' => array( new NotBlank())
))
->add('password',TextType::class,array(
'constraints' => array( new NotBlank())
))
->add('remember',CheckboxType::class)
->getForm();
$request = Request::createFromGlobals();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
var_dump($data);
die();
}
echo $twig->render('login.html.twig', array(
'form' => $form->createView()
));
The twig template:
<!doctype html>
<html lang="it">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<title>Test</title>
<!-- Bootstrap core CSS -->
<link href="app/template/asset/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="app/template/asset/css/signin.css" rel="stylesheet">
</head>
<body class="text-center">
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" />
{{ form_end(form) }}
</body>
</html>
Can you help me?
Thanks.