2

I have a ProfileType as follows:

namespace Site\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ProfileType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {       
        $builder->add('facebook', 'text', array('required'=>false))
            ->add('myspace', 'text', array('required'=>false))
            ->add('twitter', 'text', array('required'=>false))
            ->add('soundcloud', 'text', array('required'=>false))
            ->add('youtube', 'text', array('required'=>false))
            ->add('website', 'text', array('required'=>false))
            ->add('bio', 'textarea', array('required'=>false));
    }

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

and I want to pre populate the form fields with data that is already in the database so it is visible in the form.

My controller:

namespace Site\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Site\UserBundle\Entity\Profile;
use Site\UserBundle\Form\Type\ProfileType;

class ProfileController extends Controller
{
    public function editAction()
    {           
        $em = $this->getDoctrine()->getEntityManager();
        $editprofile = $em->getRepository('SiteUserBundle:Profile')->findOneByUser($user = $this->get('security.context')->getToken()->getUser()->getId());

        $form = $this->createForm(new ProfileType(), $editprofile);

        $form->bindRequest($this->getRequest());
            if ($form->isValid()) {
                $editprofile->setUpdated(new \DateTime("now"));
                $em->flush();

                return $this->redirect($this->generateUrl('SiteUserBundle_login'));
            }

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

Any ideas? I thought this way would be easier to update a users profile.

2
  • Please confirm 1) the $editprofile is valid and 2) put $form->getData() after $form->isvalid and check that it has the valid form data. Commented Sep 16, 2011 at 5:05
  • Yes. Everything is fine. I just want to pre populate the form input with data from the database. Maybe I should assign variables and send them to view. Commented Sep 16, 2011 at 10:55

4 Answers 4

4

Replace

findOneByUser($id)

by

find($id)

Also try passing the $id as a slug from url to your Action. url: example.com/page/id

sampleAction($id){}
Sign up to request clarification or add additional context in comments.

Comments

0

Before to bind:

$editProfile->setSomething... // the stuffs from the database
$form->setData($editProfile);

Comments

0

You should only bind the request to the form if the form was actually submitted.

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

Comments

0

Try dumping $editprofile, check what it returns, it should return all the profile contents. in your case the row for the particular id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.