Symfony News

New in Symfony 2.8: Form improvements

Added a new Range form type

Contributed by
Joshua Thijssen
in #12067.

The HTML5 range form field was missing from the list of built-in Symfony Form types. This new type is rendered as a slider in browsers that support HTML5 form controls. Use the min and max attributes to constraint the selectable values:

1
2
3
$builder->add('rating', 'Symfony\Component\Form\Extension\Core\Type\RangeType', [
    'attr' => ['min' => 0, 'max' => 10]
]);

Added the prototype_data option to collection type

Contributed by
Kristen Gilden
in #12314.

Forms that contain collections allow to customize the HTML used to add new items (prototype option) and the name of the placeholder used in that template (prototype_name option). Symfony 2.8 allows to also define the default data of each new collection row thanks to the prototype_data option:

1
2
3
4
5
$builder->add('tags', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', [
    // ...
    'by_reference' => false,
    'prototype_data' => '...',
]);

Allowed to return null for query_builder

Contributed by
Thomas Lallement
in #13990.

Sometimes it's useful to display an empty list of entries for entity form types. In Symfony 2.7 you needed to pass an empty list to its choices option. In Symfony 2.8, you can return null in the query_builder closure to display an empty list of entries:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
$event->getForm()->add('events', 'Symfony\Component\Form\Extension\Core\Type\EntityType', [
    'class' => 'AppBundle:Event',
    'property' => 'title',
    'query_builder' => function(EntityRepository $er) {
        return $er->getEventQueryBuilder();
    }
]));

// ...

public function getEventQueryBuilder()
{
    if ( ... some condition ... ) {
        return null;
    }

    $qb = $this->getEntityManager()->createQueryBuilder();

    return $qb->select(...)->...;
}

Foundation 5 form theme

Contributed by
Jean-Christophe Cuvelier
in #12587.

In Symfony 2.6 we introduced a form theme for applications which use the ubiquitous Bootstrap CSS framework. In Symfony 2.8 we expanded our CSS framework support by adding a new form theme for Foundation 5 CSS framework.

Use the form_themes configuration option to apply this theme to all your forms:

1
2
3
4
# app/config/config.yml
twig:
    form_themes:
        - 'foundation_5_layout.html.twig'

Be trained by Symfony experts - 2015-12-08 London - 2015-12-08 London - 2015-12-10 London


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook