簡體   English   中英

獨立的Symfony表單提交不起作用

[英]Standalone Symfony Forms submit not working

我正在維護非常舊的php框架,現在是時候對其進行升級以支持作曲家和symfony形式了。

我正在看非常干凈的文檔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(),
        ));
    }
}

樹枝模板看起來像:

<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>

表單似乎可以正確呈現: 在此處輸入圖片說明

我不知道我為該實現錯過了什么-好像Request沒有看到任何提交。

謝謝。

問題在於舊系統中斷了請求提交。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM