我正在努力维护非常古老的php框架,现在是时候升级它以支持composer和symfony-forms。
我正在查看非常干净的文档https://symfony.com/doc/3.4/components/form.html
但不幸的是,我无法弄清楚为什么我的表格没有提交。也许你们中间有人遇到过类似的问题。任何回应都表示赞赏。
我为我将要创建的每个新组件实现了一个适配器类,例如:
<?php
require __DIR__ . '/vendor/autoload.php';
use Symfony\Bridge\Twig\Extension\FormExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Forms;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Csrf\CsrfTokenManager;
use Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator;
use Symfony\Component\Security\Csrf\TokenStorage\NativeSessionTokenStorage;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\Validation;
/**
* Forms Framework v1 Adaptor for FormsV2
*/
class FormsV2Adaptor extends form
{
const DEFAULT_FORM_THEME = 'form_div_layout.html.twig';
const VENDOR_DIR = __DIR__ . '/vendor';
const VENDOR_FORM_DIR = self::VENDOR_DIR . '/symfony/form';
const VENDOR_VALIDATOR_DIR = self::VENDOR_DIR . '/symfony/validator';
const VIEWS_DIR = __DIR__ . '/Resources/views';
private $twig;
private $formFactory;
private $requestHandler;
private $request;
public function __construct($viewsPath = [])
{
parent::__construct();
// Make sure to disable old Templating Engine
$this->session_set('displaymode', 'formsv2');
// Set up the CSRF Token Manager
$csrfGenerator = new UriSafeTokenGenerator();
$csrfStorage = new NativeSessionTokenStorage();
$csrfManager = new CsrfTokenManager($csrfGenerator, $csrfStorage);
// Set up the Validator component
$validator = Validation::createValidator();
// the path to TwigBridge library so Twig can locate the theme
$appVariableReflection = new \ReflectionClass('\Symfony\Bridge\Twig\AppVariable');
$vendorTwigBridgeDir = dirname($appVariableReflection->getFileName());
$this->twig = new Twig_Environment(new Twig_Loader_Filesystem(
array_merge(
$viewsPath,
array(
self::VIEWS_DIR,
$vendorTwigBridgeDir . '/Resources/views/Form',
)
)
));
$formEngine = new TwigRendererEngine(array(self::DEFAULT_FORM_THEME), $this->twig);
$this->twig->addRuntimeLoader(new \Twig_FactoryRuntimeLoader(array(
FormRenderer::class => function () use ($formEngine, $csrfManager) {
return new FormRenderer($formEngine, $csrfManager);
},
)));
$this->twig->addExtension(new FormExtension());
// creates the Translator
$translator = new Translator('en');
// somehow load some translations into it
$translator->addLoader('yml', new YamlFileLoader());
$translator->addResource(
'yml',
__DIR__ . '/Resources/translations/messages.en.yml',
'en'
);
// Load validation messages.
$translator->addLoader('xlf', new XliffFileLoader());
$translator->addResource(
'xlf',
self::VENDOR_FORM_DIR . '/Resources/translations/validators.en.xlf',
'en',
'validators'
);
$translator->addResource(
'xlf',
self::VENDOR_VALIDATOR_DIR . '/Resources/translations/validators.en.xlf',
'en',
'validators'
);
// adds the TranslationExtension (gives us trans and transChoice filters)
$this->twig->addExtension(new TranslationExtension($translator));
$this->formFactory = Forms::createFormFactoryBuilder()
->addExtension(new HttpFoundationExtension())
->addExtension(new CsrfExtension($csrfManager))
->addExtension(new ValidatorExtension($validator))
->getFormFactory();
}
public function getRequest()
{
$this->request = Request::createFromGlobals();
return $this->request;
}
public function render($template, $props)
{
echo $this->twig->render($template . '.html.twig', $props);
}
public function createForm($formType = null)
{
return $this->formFactory->createBuilder($formType);
}
}
以及未来表单控制器的一个示例:
<?php
use Modules\TestingFormsV2\Type\PostType;
class TestingFormsV2 extends FormsV2Adaptor
{
public $viewsPath;
public function __construct()
{
$this->viewsPath[] = realpath(__DIR__ . '/Resources/views');
parent::__construct($this->viewsPath);
}
public function __default()
{
$request = $this->getRequest();
/** Build Form From FormType Mapped to an Entity Post */
$form = $this->createForm(PostType::class)
->getForm();
/** Handle Form Request */
$form->handleRequest($request);
if ($request->isMethod('POST')) {
$form->submit($request->request->get($form->getName()));
if ($form->isSubmitted() && $form->isValid()) {
// perform some action...
$data = $form->getData();
echo '<pre>';
print_r($data);
echo '</pre>';
die('dieBreak ' . __FILE__ . ' (' . __LINE__ . ')');
return $this->redirectToRoute('task_success');
}
}
/** Render Template */
$this->render('default', array(
'form' => $form->createView(),
));
}
}
Twig Template看起来像:
<html>
<head>
<title>Standalone Form Component</title>
</head>
<body>
<h1>Testing Simple Form</h1>
<p>{{ 'forms_framework'|trans }}</p>
{{ form_start(form) }}
{{ form_widget(form) }}
<input type="submit" />
{{ form_end(form) }}
</body>
</html>
我不明白我为此实现错过了什么 - 看起来像请求没有看到任何提交。
谢谢。
答案 0 :(得分:0)
问题在于旧的系统中断请求提交。