Form Parts & Functions Reference

Yo peeps! It's time to jump into a topic that's actually, super fun! Yep, we're going to learn to bend Symfony forms to our will: controlling exactly how they render... and believe me, by the end of this course, you'll be able to render a field in whatever weird way you want to.

Grab the Course Code!

To make forms great again, let's code together! Download the code from this page and unzip it. Inside, you'll find a trusty start/ directory, which will hold the exact code that I already have here.

To get the project running, open the file and follow all the amazing details there. The last step will be open a terminal, move into the project directory - mine is called aqua_note - and then start the built-in PHP web server with:

bin/console server:run

Find a browser and pull up the address - http://localhost:8000 - to find our awesome project: Aquanote!

For this tutorial, there's just one thing you need to know: our database has a genus table, which is a type of animal classification. That table holds a bunch of different types of sea animals. To manage this, we have an admin section: login with and password iliketurtles.

The admin section lives at /admin/genus. Click edit and... here's our starting form!

Form Type Basics

And so far, our form has all the parts you'd expect: a form class: GenusFormType:

50 lines src/AppBundle/Form/GenusFormType.php
... lines 1 - 13
class GenusFormType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
->add('subFamily', EntityType::class, [
'placeholder' => 'Choose a Sub Family',
'class' => SubFamily::class,
'query_builder' => function(SubFamilyRepository $repo) {
return $repo->createAlphabeticalQueryBuilder();
->add('isPublished', ChoiceType::class, [
'choices' => [
'Yes' => true,
'No' => false,
->add('firstDiscoveredAt', DateType::class, [
'widget' => 'single_text',
'attr' => ['class' => 'js-datepicker'],
'html5' => false,
public function configureOptions(OptionsResolver $resolver)
'data_class' => 'AppBundle\Entity\Genus'

And a controller that builds the form and passes it into the template:

86 lines src/AppBundle/Controller/Admin/GenusAdminController.php
... lines 1 - 15
class GenusAdminController extends Controller
... lines 18 - 34
public function newAction(Request $request)
$form = $this->createForm(GenusFormType::class);
// only handles data on POST
if ($form->isSubmitted() && $form->isValid()) {
$genus = $form->getData();
$em = $this->getDoctrine()->getManager();
sprintf('Genus created by you: %s!', $this->getUser()->getEmail())
return $this->redirectToRoute('admin_genus_list');
return $this->render('admin/genus/new.html.twig', [
'genusForm' => $form->createView()
... lines 60 - 85

This gives us a genusForm variable inside of new.html.twig:

14 lines app/Resources/views/admin/genus/new.html.twig
{% extends 'admin/genus/formLayout.html.twig' %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1>New Genus</h1>
{{ include('admin/genus/_form.html.twig') }}
{% endblock %}

But the real work is done via an included template: _form.html.twig:

13 lines app/Resources/views/admin/genus/_form.html.twig
{{ form_start(genusForm) }}
{{ form_row( }}
{{ form_row(genusForm.subFamily) }}
{{ form_row(genusForm.speciesCount, {
'label': 'Number of Species'
}) }}
{{ form_row(genusForm.funFact) }}
{{ form_row(genusForm.isPublished) }}
{{ form_row(genusForm.firstDiscoveredAt) }}
<button type="submit" class="btn btn-primary" formnovalidate>Save</button>
{{ form_end(genusForm) }}

Let's start there.

The Form Rendering Functions

The genusForm variable is an object, but you can't just print it. Instead, Symfony gives us a bunch of form functions: each renders a different part of the form.

To get all the deets, head to Click into the Documentation and then find the Reference section. This holds a wonderful page called Twig Template Function and Variable Reference. This lists all the functions we'll be using and their arguments. Let's dive into these... and then, extend the heck out of them.

Leave a comment!

  • 2016-12-04 somecallmetim27

    Lol. Well played

  • 2016-12-03 weaverryan

    The forms are going to be HUUUUGE!

    No political preference or allegiance in any direction intended - just having some fun :)

  • 2016-12-03 somecallmetim27

    A Donald Trump reference? Really?