Symfony 2 prefilled form with data from the database

I am trying to populate form fields with data from the database in order to edit them. I already searched on google.

Here is my controller which returns empty fields

public function userViewAction($id,Request $request){

    $em = $this->getDoctrine()->getManager()->getRepository('BFVMailingBundle:MailingList');
    $user = $em->findById($id);

    $form = $this->get('form.factory')->createBuilder('form',$user)
        ->add('unsubscribed','checkbox')
        ->add('name','text')
        ->add('givenName','text')
        ->add('additionalName','text',array('required'=>false))
        ->add('familyName','text',array('required'=>false))
        ->add('emailValue','text')
        ->add('language','choice',array(
            'choices'  => array('en_GB' => 'en_GB', 'es_ES' => 'es_ES', 'fr_FR' => 'fr_FR'),
            'required' => true,
        ))
        ->add('commentary','textarea',array('required'=>false))
        ->add('save','submit')
        ->getForm();


    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            // perform some action, such as save the object to the database
            $em->flush();
            return $this->redirect($this->generateUrl('user_view',array('id'=>$id)));
        }
    }

and this is my template

<div class="cell">  
    {{ form_start(form, {'attr': {'class': 'form-horizontal'}}) }}
    {{ form_end(form) }}
</div>

Did I miss something?

EDIT - READ THIS FOR THE SOLUTION

As John Noel Implied I build an externalised form with the command

php app/console doctrine:generate:form BFVMailingBundle:MailingList

my entity was MailingList instead of User

the MailingListType is a form template which is generated in BFV\MailingBundle\Form. I've added the data types myself.

<?php
namespace BFV\MailingBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class MailingListType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $str = date("U");
    $codeValue = sha1($str);

    $builder
        ->add('secretCode','hidden',array( 'data' => $codeValue ))
        ->add('name','text')
        ->add('givenName','text')
        ->add('additionalName','text',array('required'=>false))
        ->add('familyName','text',array('required'=>false))
        ->add('emailValue','text')
        ->add('language','choice',array(
            'choices'  => array('en_GB' => 'en_GB', 'es_ES' => 'es_ES', 'fr_FR' => 'fr_FR'),
            'required' => true,
        ))
        ->add('unsubscribed','checkbox')
        ->add('commentary','textarea',array('required'=>false))
        ->add('save','submit')
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'BFV\MailingBundle\Entity\MailingList'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'bfv_mailingbundle_mailinglist';
}
}

In the reformated controller I add to add the the form generator the instance of MailingList $user[0] instead of $user. I read in many websites that usually you put $variable directly in the form builder but that generated the following error:

The form's view data is expected to be an instance of class 
BFV\MailingBundle\Entity\MailingList, but is a(n) array. You can avoid 
this error by setting the "data_class" option to null or by adding a
view transformer that transforms a(n) array to an instance of 
BFV\MailingBundle\Entity\MailingList

Thus in the controller:

    public function userViewAction($id,Request $request){
    if (!$id) {
        throw $this->createNotFoundException('No id !!');
    }

    $em = $this->getDoctrine()->getManager()->getRepository('BFVMailingBundle:MailingList');
    $user = $em->findById($id);
    if (!$user){
        throw $this->createNotFoundException('No user with the id selected');
    }

    $form = $this->createForm(new MailingListType(), $user[0]);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em->flush();
            return $this->redirect($this->generateUrl('user_view',array('id'=>$id)));
        }
    }

    return $this->render('BFVMailingBundle:Default:user_view.html.twig',array(
        'user'=>$user,
        'form'=>$form->createView()
            ));
}

Conclusion: I got the view form rendering with populated data from the database.

Upvotes: 0

Views: 1633

Answers (2)

user4545769
user4545769

Reputation:

To do this you'll want to look into form classes which will then act as a view (and will also populate) the data you provide. So in your example you'd create a form class UserType:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('unsubscribed','checkbox')
            ->add('name','text')
            ->add('givenName','text')
            ->add('additionalName','text',array('required'=>false))
            ->add('familyName','text',array('required'=>false))
            ->add('emailValue','text')
            ->add('language','choice',array(
                'choices'  => array('en_GB' => 'en_GB', 'es_ES' => 'es_ES', 'fr_FR' => 'fr_FR'),
                'required' => true,
            ))
            ->add('commentary','textarea',array('required'=>false))
            ->add('save','submit')
        ;
    }

    public function getName()
    {
        return 'user';
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Your\Entity\Class',
        ));
    }
}

Then within your controller you'd do something along the lines of:

$form = $this->createForm(new UserType(), $user);

Then the rest of your controller as you have. Definitely read up on form classes though as that's the starting point for a lot of the advanced functionality of Symfony forms.

Upvotes: 1

Dric512
Dric512

Reputation: 3731

I'm not sure if this is what makes the difference, but did you try with:

$form = $this->createFormBuilder($user)
             ->add(...)
             ->getForm()

You may also check that the User you get is correctly read from the DB.

For example:

if (!is_object($user)) {
   $this->createNotFoundException('The user does not exist');
}

Upvotes: 0

Related Questions