简体   繁体   中英

Uncaught exception 'Twig_Error_Syntax' with message 'Unknown “trans” filter'

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.

Just reading the error you can tell what exactly is wrong:

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

which means file bootstrap_4_layout.html.twig -actually comes form symfony twig-bridge- has something like {{ string|trans ...}} this is mostly used for multi languages and since you got this error then this mean your trans filter is not enabled and you need to enable it!

And from the same tutorial you are following it says:

Translation

If you're using the Twig integration with one of the default form theme files (eg form_div_layout.html.twig), there are 2 Twig filters (trans and transChoice) that are used for translating form labels, errors, option text and other strings.

so just follow the next steps and your problem should be solved

  • composer require symfony/translation symfony/config
  • Next, add the TranslationExtension to your Twig_Environment instance

refer to this: https://symfony.com/doc/3.4/components/form.html#component-form-intro-install-translation

I think you forgot to add the filter to the new environment :

$getTextdomain = new \Twig_SimpleFilter('trans',function ($string){

    return $this->container->get('translator')->trans($string);    

 });

$twig->addFilter($getTextdomain);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM