Symfony form handleRequest not populating

overburn

I have a symfony 3 form. And I'm trying to populate it with handleRequest like this:

$user = new User();
$userForm = $this->createForm(UserType::class, $user);
print_r($request->request->all());
$userForm->handleRequest($request);

print_r($userForm->getData());

The output I get is:

Array
(
    [firstName] => test
    [last_name] => test
    [email] => [email protected]
    [password] => erkeferke
    [gender] => F
    [supervisor] => 1
)
AppBundle\Entity\User Object
(
    [id:AppBundle\Entity\User:private] => 
    [firstName] => 
    [lastName:AppBundle\Entity\User:private] => 
    [email:AppBundle\Entity\User:private] => 
    [password:AppBundle\Entity\User:private] => 
    [photo:AppBundle\Entity\User:private] => 
    [gender:AppBundle\Entity\User:private] => 
    [supervisor:AppBundle\Entity\User:private] => 
    [duties:AppBundle\Entity\User:private] => 
    [lastLogin:AppBundle\Entity\User:private] => 
    [createdAt:AppBundle\Entity\User:private] => 
    [updatedAt:AppBundle\Entity\User:private] => 
    [deletedAt:AppBundle\Entity\User:private] => 
)

Any idea why it's not populating?

Samundra

To automatically populate entity from form request, you have to bind your entity to the FormType class. You might be missing this bind from your UserType class. In form UserType you should add a method configureOptions inside it you specify the entity class name in data_class key. To have more insights into it see Symfony Forms and scroll down to Setting the data_class topic. Also See example below where I have listed the usage.

<?php namespace AppBundle\Form;

...

class SiteType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
       // define form fields
    }


    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\Site', // Entity to resolve to
        ));
    }
}

Now, when you use the form, your entity is resolved from the request as shown below $site = $form->getData() will return Site entity. This is quite tricky to get right at the first time. You have to practice it a lot until you get hang of it. Let us know if you have confusions following it.

 /**
 *
 * @param Request $request
 *
 * @return \Symfony\Component\HttpFoundation\Response
 *
 * @Route("/sites/create", name="_create_site")
 */
public function createSite(Request $request)
{
    $site = new Site;
    $form = $this->createForm(SiteType::class, $site);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {           
        $site = $form->getData();  // It will return Site Entity

        ...

        $this->addFlash('success', 'Record added successfully.');
    }

    return $this->render('sites/create.html.twig', [
        'site_form' => $form->createView(),
    ]);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony - Unable to handleRequest in form

From Dev

Symfony: call form handleRequest but avoid entity persist

From Dev

Obscure error on Symfony on form->handleRequest() and validation

From Dev

Symfony 2.3 validating form with handleRequest is insanely slow

From Dev

symfony - getting logged out at form->handleRequest

From Dev

Symfony2 - How to stop Form->handleRequest from nulling fields that don't exist in post data

From Dev

How do I specify the EntityManager used by Symfony2 Form->handleRequest?

From Dev

How do I specify the EntityManager used by Symfony2 Form->handleRequest?

From Dev

Symfony handleRequest() not matching POST data

From Dev

Symfony2 DataTransformer after handleRequest

From Dev

Symfony2 Undefined variable: handleRequest

From Dev

populating a dynamic form with javascript

From Dev

populating form with mysql data

From Dev

populating a dynamic form with javascript

From Dev

handleRequest($request) does not work for "GET" method in Symfony 2

From Dev

handleRequest($request) does not work for "GET" method in Symfony 2

From Dev

Re-populating form field

From Dev

Cocoon populating form in reverse order

From Dev

Re-populating form field

From Dev

Populating a calculated value into another form

From Dev

Ajax dont works populating a form

From Dev

Hidden attribute not populating field in form for Rails 3.2

From Dev

Pre-Populating form data using liftweb

From Dev

Populating AngularJS Form from multiple data sources

From Dev

Rails: belongs_to association through a form not populating

From Dev

Simple form date_field not populating for edit

From Dev

Populating form fields with query from 3 tables

From Dev

Form submit after populating checkbox through ajax

From Dev

creating a form and populating it with values in database in django

Related Related

  1. 1

    Symfony - Unable to handleRequest in form

  2. 2

    Symfony: call form handleRequest but avoid entity persist

  3. 3

    Obscure error on Symfony on form->handleRequest() and validation

  4. 4

    Symfony 2.3 validating form with handleRequest is insanely slow

  5. 5

    symfony - getting logged out at form->handleRequest

  6. 6

    Symfony2 - How to stop Form->handleRequest from nulling fields that don't exist in post data

  7. 7

    How do I specify the EntityManager used by Symfony2 Form->handleRequest?

  8. 8

    How do I specify the EntityManager used by Symfony2 Form->handleRequest?

  9. 9

    Symfony handleRequest() not matching POST data

  10. 10

    Symfony2 DataTransformer after handleRequest

  11. 11

    Symfony2 Undefined variable: handleRequest

  12. 12

    populating a dynamic form with javascript

  13. 13

    populating form with mysql data

  14. 14

    populating a dynamic form with javascript

  15. 15

    handleRequest($request) does not work for "GET" method in Symfony 2

  16. 16

    handleRequest($request) does not work for "GET" method in Symfony 2

  17. 17

    Re-populating form field

  18. 18

    Cocoon populating form in reverse order

  19. 19

    Re-populating form field

  20. 20

    Populating a calculated value into another form

  21. 21

    Ajax dont works populating a form

  22. 22

    Hidden attribute not populating field in form for Rails 3.2

  23. 23

    Pre-Populating form data using liftweb

  24. 24

    Populating AngularJS Form from multiple data sources

  25. 25

    Rails: belongs_to association through a form not populating

  26. 26

    Simple form date_field not populating for edit

  27. 27

    Populating form fields with query from 3 tables

  28. 28

    Form submit after populating checkbox through ajax

  29. 29

    creating a form and populating it with values in database in django

HotTag

Archive