My name is  
Jurgens du Toit.
Systems Developer.
Problem Solver.

About Me

Technology and Solving Problems are my passion. I'm a South African that loves my wife, life, and coding.

I'm writing a book!

The Logstash Config Guide

Buy it now on Leanpub

03 February 2014

Populate a Symfony 2 Form with the referring entity

By Jurgens du Toit

In any web project it often happens that you have an entity, say a Group, to which you want to add a linked entity, say a Student. So you’ll have a “Add a Student” link on the page displaying the Group. When your users click through, there’s probably a dropdown with the different groups, and your users expect the Group they’re coming from to be prepopulated.

There’s various ways to do this, but I’d like to show you a simple, non intrusive one for Symfony 2. You don’t have to add any parameters to the link, it just works. In your Student Controller, the newAction method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
// src/My/Bundle/Controller/StudentController.php
    public function newAction()
    {
        $entity = new Student();
        $form   = $this->createForm(new StudentType(), $entity);

        // Get the refering URL Path
        $ref = str_replace("app_dev.php/", "", parse_url($request->headers->get('referer'),PHP_URL_PATH ));
        // Get the matching route
        $route = $this->container->get('router')->match($ref);
        if (empty($route['_route']) === false && $route['_route'] === 'group_show') {
            // Find the referring group
            if ($group = $this->get('doctrine')->getRepository('MyBundle:Group')->findOneById($route['id'])) {
                $form->get('group')->setData($group);
            }
        }

        return $this->render('MyBundle:Student:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }
?>

We basically try and match the referring URL to a route, and if found, retrieve that entity, and set it in the form. Simple!

blog comments powered by Disqus