New in Symfony 6.1: Customizable Collection Prototypes

Contributed by Michael Käfer in #45605.

The Symfony Form component is so stable and provides so many features, that we rarely add new features to it. However, in Symfony 6.1 we're improving its developer experience with a new feature to customize collection prototypes. The CollectionType form field is used to render a collection of other fields or forms. When the field allows to add new elements to the collection, it defines a prototype option. This contains the HTML code needed to render the new empty collection item (you've probably seen it as the data-prototype HTML attribute of the element that wraps the entire collection). The only drawback is that collection prototypes are not entirely configurable. You can set their initial values with the prototype-data option, but that's pretty much it. That's why in Symfony 6.1 we're adding a new option called prototype_options so you can configure collection prototypes. The options defined in prototype_options are passed to the form type specified in the entry_type option when creating its prototype. In practice, this allows to have different options depending on whether you are adding a new entry or editing an existing entry:

    $builder->add('names', CollectionType::class, [
'entry_type'   => TextType::class,

// this is used when editing items in the collection
'entry_options'  => [
    'attr' => ['class' => 'item-edit'],
    'help'  => 'You cannot edit existing names.',
    'disabled' => true,
],

// this is used when adding new items to the collection
'prototype_options'  => [
    'attr' => ['class' => 'item-add'],
    'help'  => 'Check out the rules to create new names',
    'help_html' => true,
],

]);

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-6-1-customizable-collection-prototypes?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Created 3y | May 9, 2022, 8:20:08 AM


Login to add comment

Other posts in this group

A Week of Symfony #961 (May 26 – June 1, 2025)

This week, Symfony released the stable version of Symfony 7.3, which includes lots of amazing new features. We also published the maintenance versions 6.4.22 and 7.2.7.

Symfony development highlights

Jun 1, 2025, 8:50:16 AM | Symfony
New in Symfony 7.3: DX Improvements (part 2)

This is the second part of the blog post showcasing the main DX (developer experience) features introduced in Symfony 7.3. Read the first part of this blog post.

Verify URI Signatures… https://symfon

May 29, 2025, 9:10:19 AM | Symfony
Symfony 6.4.22 released

Symfony 6.4.22 has just been released. Read the Symfony upgrade guide to learn more about upgrading Symfony and use the SymfonyInsight upgrade reports to detect the code you will need to change in you

May 29, 2025, 9:10:19 AM | Symfony
Symfony 7.2.7 released

Symfony 7.2.7 has just been released. Read the Symfony upgrade guide to learn more about upgrading Symfony and use the SymfonyInsight upgrade reports to detect the code you will need to change in your

May 29, 2025, 9:10:18 AM | Symfony
Symfony 7.3.0 released

Symfony 7.3.0 has just been released. Check the New in Symfony 7.3 posts on this blog to learn about the main features of this new stable release; or check the first beta release announcement to get t

May 29, 2025, 9:10:17 AM | Symfony
Symfony 7.3 curated new features

Symfony 7.3.0 has been released. As for any other Symfony release, our backward compatibility promise applies and this means that you should be able to upgrade easily to 7.3 without changing anything

May 29, 2025, 9:10:16 AM | Symfony
New in Symfony 7.3: DX Improvements (part 1)

Symfony 7.3 includes many small improvements aimed at making developers' lives easier and more productive. This blog post highlights some of the most useful DX (Developer Experience) features added in

May 28, 2025, 9:50:15 AM | Symfony