简体   繁体   中英

What should I pass to handleRequest class of symfony?

I am using this twig and standalone symfony form and validator component:

use Symfony\Component\Validator\Constraints as Assert;
// other use lines ommitted to shorten the code.

$defaultFormTheme = 'bootstrap_4_horizontal_layout.html.twig';

$csrfGenerator = new UriSafeTokenGenerator();
$csrfStorage = new NativeSessionTokenStorage();
$csrfManager = new CsrfTokenManager($csrfGenerator, $csrfStorage);

$formEngine = new TwigRendererEngine([$defaultFormTheme], $twig);
$twig->addRuntimeLoader(new FactoryRuntimeLoader([
    FormRenderer::class => function () use ($formEngine, $csrfManager) {
        return new FormRenderer($formEngine, $csrfManager);
    },
]));
$twig->addExtension(new FormExtension());

$translator = new Translator('fr_FR');
$translator->addLoader('php', new \Symfony\Component\Translation\Loader\PhpFileLoader());
$translator->addResource('php', ROOT.'/translations/messages.fr.php', 'fr_FR');
$twig->addExtension(new TranslationExtension($translator));

$validator = Validation::createValidator();

$formFactory = Forms::createFormFactoryBuilder()
    ->addExtension(new CsrfExtension($csrfManager))
    ->addExtension(new ValidatorExtension($validator))
    ->getFormFactory();

$form = $formFactory->createBuilder(FormType::class, null, ['csrf_protection' => false])
    ->add('firstnameEn', TextType::class, [
            'constraints' => [new Assert\Length(['min' => 3])]
        ])
    ->add('lastnameEn', TextType::class)
    ->add('email', EmailType::class)
    ->add('birthDate', TextType::class)
    ->add('password', PasswordType::class)
    ->add('applyCard', CheckboxType::class)
    ->add('showPhoto', CheckboxType::class)
    ->add('privacyRead', CheckboxType::class)
    ->getForm();

$request = Request::createFromGlobals();
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

   $errors = $form->getErrors();
   var_dump($errors);
   $data = $form->getErrors();
   var_dump($data);
   print("debug pring");

} else {

   $errors = $form->getErrors();
   var_dump($errors);
   $data = $form->getErrors();
   var_dump($data);
   print("debug pring");

}

echo $twig->render('signup.html', 
['form' => $form->createView(),
 'title' => 'title',
]);

I am getting

Expected argument of type "null", "Symfony\Component\HttpFoundation\Request" given

that is because of

$form->handleRequest($request);

Why am I getting this error and how to fix it? when I use just

$form->handleRequest();

it works, but of course then form validation is not working. How to fix it? What should I pass to its constructor? II didn't want to use its isSubmitted() if clause and had no need to validate it and I wanted to use just to output the form it was working fine. The problem appeared when I wanted to use HTTFoundation to use isSubmitted if clause. What should I do?

The Form component uses request handlers ( symfony docs ) to handle requests in the handleRequest() method. From the linked documentation:

To process form data, you'll need to call the handleRequest() method:

 $form->handleRequest();

Behind the scenes, this uses a NativeRequestHandler object to read data off of the correct PHP superglobals (ie $_POST or $_GET ) based on the HTTP method configured on the form (POST is default).

If you want to use the Request object from the HttpFoundation, you might want to configure the Form component to us the HttpFoundationRequestHandler instead. In that case, you have to pass the $request argument to the handleRequest() method.

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