V
V
Vladislav2017-03-03 00:16:29
symfony
Vladislav, 2017-03-03 00:16:29

How to create a form from request data in symfony form component?

Reading the documentation on how to use the form component symfony.com/doc/current/components/form.html. I use exactly the component, i.e. outside the framework as standalone.

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;

$formFactory = Forms::createFormFactoryBuilder()
    ->addExtension(new HttpFoundationExtension())
    ->getFormFactory();

$form = $formFactory->createBuilder()
    ->add('email', EmailType::class)
    ->add('pass', PasswordType::class)
    ->getForm();

$request = Request::createFromGlobals();

$form->handleRequest($request);

$data = $form->getData();

And for some reason $form->getData(); returns null. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2017-03-03
@MrBikus

The form must be rendered based on the FormView
symfony.com/doc/current/components/form.html#rende...
For the component expects, for example, not $_POST['email'] and $_POST['pass'], but $_POST ['{form-name}']['email'] and $_POST['{form-name}']['pass']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question