新增全新的Range表单类型 

Contributed by
Joshua Thijssen
in #12067.

HTML5的range表单字段被Symfony内置的表单类型列表给遗失了。这个全新的表单类型,在浏览器中被渲染为一个滑块(slider)以支持HTML5的表单控制。使用minmax属性来约束可选数值。

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

为collection类型增加prototype_data选项 

Contributed by
Kristen Gilden
in #12314.

拥有集合(collections)的表单允许自定义用于添加新元素的HTML(prototype选项),以及用于该模板的占位符名称(prototype_name选项)。在Symfony 2.8中,得益于prototype_data选项,允许自定义集合中每一个新行的默认数据

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

为date-相关类型增加choice_domain选项 

Contributed by
Abdellatif Ait boudad
in #15301.

在Symfony 2.7中,我们增加了choice_translation_domain选项,用于定义“choice值如何被翻译”。在Symfony 2.8中,这个选项也被应用于date相关类型之中了(DateTypeTimeTypeDateTimeType):

1
2
3
4
$builder->add('publishedAt', 'Symfony\Component\Form\Extension\Core\Type\DateTimeType', [
    // ...
    'choice_translation_domain' => 'messages',
]);

choice_translation_domain选项的取值可以是下面几类:

  • true = 使用当前翻译域(translation domain);

  • false = 不翻译当前值;

  • null = 使用父级翻译域或默认域;

  • 任何字符串 = 使用确切的translation domain。

允许query_builder返回null 

Contributed by
Thomas Lallement
in #13990.

有时,显示一个entity表单类型的空list是有用的(译注:这里的list是指用于select或checkbox、radio等处的list)。在Symfony 2.7中你需要传递一个空list给choices选项。到了Symfony 2.8,你只需在query_builder闭包中返回null,即可显示空list。

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表单主题 

Contributed by
Jean-Christophe Cuvelier
in #12587.

在Symfony 2.6中,我们引入了表单主题,用于那些使用了无所不在的Bootstrap CSS框架的程序。对于Symfony 2.8,我们扩展了对于CSS前端框架的支持,增加了一个全新的Foundation 5表单主题

使用form_themes配置选项,即可在你的全部表单中开启该主题:

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