我遇到了Twig和Symfony Form组件的问题。我在我的项目中集成了Form和Twig(我使用PHP 5.5.9,没有计划更新,所以我在3.4.4版本中使用了所有Symfony所需的组件)
我遵循本指南:https://symfony.com/doc/3.4/components/form.html
在页面渲染中,我收到以下错误:
致命错误:未捕获的异常' Twig_Error_Syntax'消息'未知" trans" 。滤波器#&39; in /symfony/twig-bridge/Resources/views/Form/bootstrap_4_layout.html.twig:158堆栈跟踪
这是我的代码:
使用部分:
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;
用于呈现表单的代码:
$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()
));
树枝模板:
<!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>
你能帮助我吗?
感谢。
答案 0 :(得分:1)
只是阅读错误,你可以知道到底出了什么问题:
致命错误:未捕获的异常'Twig_Error_Syntax',消息'Unknown'trans“filter”。 in /symfony/twig-bridge/Resources/views/Form/bootstrap_4_layout.html.twig:158堆栈跟踪
这意味着文件bootstrap_4_layout.html.twig
- 实际上来自symfony twig-bridge-有类似{{ string|trans ...}}
的东西,这主要用于多种语言,因为你有这个错误,这意味着你的trans过滤器没有启用你需要启用它!
从你跟随它的同一个教程中说:
翻译
如果您正在使用Twig与其中一个集成 默认表单主题文件(例如form_div_layout.html.twig),有2个 用于翻译的Twig过滤器(trans和transChoice) 表单标签,错误,选项文本和其他字符串。
所以只需按照以下步骤操作即可解决问题
composer require symfony/translation symfony/config
参考: https://symfony.com/doc/3.4/components/form.html#component-form-intro-install-translation
答案 1 :(得分:0)
我想您忘了将过滤器添加到新环境中:
$getTextdomain = new \Twig_SimpleFilter('trans',function ($string){
return $this->container->get('translator')->trans($string);
});
$twig->addFilter($getTextdomain);